Categories
Misc

Learn TensorFlow

I am a bit new to the TensorFlow. I have following questions

  1. How to select which version to use.
  2. How different are the two versions.
  3. what could be the best way to learn TensorFlow( are learning TF1 and learning TF2 completely different things?)

I would really appreciate any help. Thank you

submitted by /u/Pranavkulkarni08
[visit reddit] [comments]

Categories
Misc

Batch the calculation to resize the image

Hi,

โ€‹

Could anyone please give me an example of how to batch the calculation when using tf.image.resize? Resizing all the images (20000) in my dataset at once crashes my system. I’m trying to upscale my images. I couldn’t find the documentation or examples for it.

submitted by /u/aark91
[visit reddit] [comments]

Categories
Misc

Is the combination of high batch size and low epoch the same as that of low batch size and low epoch?

Sorry for the mistake in Question,

Is the combination of high batch size and low epoch the same as that of low batch size and high epoch?

Hey! I was just wondering …

If I try to generate High-resolution(128,128,3) images from Low resolution(32,32,3) images using GAN on a dataset of 1000 Images by following two methods :

Result_A: I run 100,000 epoch and batch_size =1 then my model will train on a total of 100,000 images. And this one image per epoch is selected randomly.

Result_B: I run 100 epochs on batch_size = 1000, then also my model trains on a total of 100,000 images.

Would Result_A be similar to Result_B? If not, why?

The differentiating factor is that in Result_B my model goes through all images, but it might not go through all images. Will this create a significant difference?

submitted by /u/shiami9
[visit reddit] [comments]

Categories
Misc

deeplab implementation, not working

Hello,

My code :

https://gofile.io/d/mTlna9

i’m beginner with tensorflow, and i’m trying to make a deeplab implementation in tensorflow 2.

I run my notebook and i don’t have error during the fit when i use run_eagerly = True, but with this option the loss stay at nan

And when i don’t use run_eagerly, i have this error :

————————————————————————— ValueError Traceback (most recent call last) <ipython-input-16-f5ffc11fb617> in <module> 133 dataset_entrain = dataset_entrain.shuffle(buffer_size=10).batch(batch_size=1) 134 –> 135 model.fit(dataset_entrain, epochs=10, callbacks=[ModelCheckpoint(‘policy_seg_{epoch}.h5’)]) 136 137 ~/envs/project/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing) 1098 _r=1): 1099 callbacks.on_train_batch_begin(step) -> 1100 tmp_logs = self.train_function(iterator) 1101 if data_handler.should_sync: 1102 context.async_wait() ~/envs/project/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds) 826 tracing_count = self.experimental_get_tracing_count() 827 with trace.Trace(self._name) as tm: –> 828 result = self._call(*args, **kwds) 829 compiler = “xla” if self._experimental_compile else “nonXla” 830 new_tracing_count = self.experimental_get_tracing_count() ~/envs/project/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds) 886 # Lifting succeeded, so variables are initialized and we can run the 887 # stateless function. –> 888 return self._stateless_fn(*args, **kwds) 889 else: 890 _, _, _, filtered_flat_args = ~/envs/project/lib/python3.8/site-packages/tensorflow/python/eager/function.py in __call__(self, *args, **kwargs) 2939 with self._lock: 2940 (graph_function, -> 2941 filtered_flat_args) = self._maybe_define_function(args, kwargs) 2942 return graph_function._call_flat( 2943 filtered_flat_args, captured_inputs=graph_function.captured_inputs) # pylint: disable=protected-access ~/envs/project/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs) 3359 3360 self._function_cache.missed.add(call_context_key) -> 3361 graph_function = self._create_graph_function(args, kwargs) 3362 self._function_cache.primary[cache_key] = graph_function 3363 ~/envs/project/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes) 3194 arg_names = base_arg_names + missing_arg_names 3195 graph_function = ConcreteFunction( -> 3196 func_graph_module.func_graph_from_py_func( 3197 self._name, 3198 self._python_function, ~/envs/project/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes) 988 _, original_func = tf_decorator.unwrap(python_func) 989 –> 990 func_outputs = python_func(*func_args, **func_kwargs) 991 992 # invariant: `func_outputs` contains only Tensors, CompositeTensors, ~/envs/project/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds) 632 xla_context.Exit() 633 else: –> 634 out = weak_wrapped_fn().__wrapped__(*args, **kwds) 635 return out 636 ~/envs/project/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs) 975 except Exception as e: # pylint:disable=broad-except 976 if hasattr(e, “ag_error_metadata”): –> 977 raise e.ag_error_metadata.to_exception(e) 978 else: 979 raise ValueError: in user code: /envs/project/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:805 train_function * return step_function(self, iterator) /envs/project/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:795 step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) /envs/project/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1259 run return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) /envs/project/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) /envs/project/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica return fn(*args, **kwargs) /envs/project/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:788 run_step ** outputs = model.train_step(data) /envs/project/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:754 train_step y_pred = self(x, training=True) /envs/project/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1012 __call__ outputs = call_fn(inputs, *args, **kwargs) <ipython-input-16-f5ffc11fb617>:83 call ** x = keras.layers.Conv2D(64, 7, strides=2, padding=”same”)(inputs) /envs/project/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1008 __call__ self._maybe_build(inputs) /envs/project/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:2710 _maybe_build self.build(input_shapes) # pylint:disable=not-callable /envs/project/lib/python3.8/site-packages/tensorflow/python/keras/layers/convolutional.py:198 build self.kernel = self.add_weight( /envs/project/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:623 add_weight variable = self._add_variable_with_custom_getter( /envs/project/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py:805 _add_variable_with_custom_getter new_variable = getter( /envs/project/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer_utils.py:130 make_variable return tf_variables.VariableV1( /envs/project/lib/python3.8/site-packages/tensorflow/python/ops/variables.py:260 __call__ return cls._variable_v1_call(*args, **kwargs) /envs/project/lib/python3.8/site-packages/tensorflow/python/ops/variables.py:206 _variable_v1_call return previous_getter( /envs/project/lib/python3.8/site-packages/tensorflow/python/ops/variables.py:67 getter return captured_getter(captured_previous, **kwargs) /envs/project/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3332 creator return next_creator(**kwargs) /envs/project/lib/python3.8/site-packages/tensorflow/python/ops/variables.py:67 getter return captured_getter(captured_previous, **kwargs) /envs/project/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3332 creator return next_creator(**kwargs) /envs/project/lib/python3.8/site-packages/tensorflow/python/ops/variables.py:67 getter return captured_getter(captured_previous, **kwargs) /envs/project/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3332 creator return next_creator(**kwargs) /envs/project/lib/python3.8/site-packages/tensorflow/python/ops/variables.py:67 getter return captured_getter(captured_previous, **kwargs) /envs/project/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py:730 invalid_creator_scope raise ValueError( ValueError: tf.function-decorated function tried to create variables on non-first call.

โ€‹

You can find my code in the link https://gofile.io/d/mTlna9 , what can i do to debug my code ?

Thank you for your advice

submitted by /u/Tensor_Beginner
[visit reddit] [comments]

Categories
Offsites

CodeReading – 3. ABC

Code Reading์€ ์ž˜ ์ž‘์„ฑ๋˜์–ด ์žˆ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ํˆดํ‚ท ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ํ”„๋กœ์ ํŠธ์˜ ๋‚ด๋ถ€๋ฅผ ์‚ดํŽด๋ณด๋Š” ์‹œ๋ฆฌ์ฆˆ ์ž…๋‹ˆ๋‹ค. ํ”„๋กœ์ ํŠธ์˜ ์•„ํ‚คํ…์ฒ˜, ๋””์ž์ธ์ฒ ํ•™์ด๋‚˜ ์ฝ”๋“œ ์Šคํƒ€์ผ ๋“ฑ์„ ์‚ดํŽด๋ณด๋ฉฐ, ๊ตฌ์ฒด์ ์œผ๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์ „๋ฐ˜์ ์ด๋ฉด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค.

Series.


์ด๋ฒˆ์— ๋‹ค๋ค„๋ณด๊ณ ์ž ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๊ธฐ๋ณธ์ ์ธ ์ถ”์ƒํ™” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” abc ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. Python์€ ๋™์  ์–ธ์–ด๋กœ์„œ, ๊ฐ์ฒด ์ง€ํ–ฅ๊ณผ ํ•จ์ˆ˜ํ˜• ๋“ฑ ๋‹ค์–‘ํ•œ ํŒจ๋Ÿฌ๋‹ค์ž„์„ ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. abc ๋ชจ๋“ˆ์€ ์—ฌ๊ธฐ์„œ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์•ˆ์„ ์‚ดํŽด๋ณด๊ธฐ ์ „์— ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ฐ์ฒด ์ง€ํ–ฅ์—์„œ์˜ ์ถ”์ƒํ™”์— ๋Œ€ํ•ด์„œ ์งš๊ณ  ๋„˜์–ด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ฐ์ฒด์ง€ํ–ฅ์—์„œ์˜ ์ถ”์ƒํ™”

์ถ”์ƒํ™”์— ๋Œ€ํ•ด์„œ ยซ์˜ค๋ธŒ์ ํŠธยป์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ์ด์•ผ๊ธฐ๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ถ”์ƒํ™”๋ž€ ์–ด๋–ค ์–‘์ƒ, ์„ธ๋ถ€์‚ฌํ•ญ, ๊ตฌ์กฐ๋ฅผ ์ข€ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ํŠน์ • ์ ˆ์ฐจ๋‚˜ ๋ฌผ์ฒด๋ฅผ ์˜๋„์ ์œผ๋กœ ์ƒ๋žตํ•˜๊ฑฐ๋‚˜ ๊ฐ์ถค์œผ๋กœ์จ ๋ณต์žก๋„๋ฅผ ๊ทน๋ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.[Kramer07]

  • ์ถ”์ƒํ™”์— ์˜์กดํ•˜๋ผ ์ค‘์—์„œ

images

์˜ค๋ธŒ์ ํŠธ์˜ ์˜ํ™”์˜ˆ๋งค ์‹œ์Šคํ…œ ์„ค๊ณ„ ์˜ˆ์‹œ (์ถœ์ฒ˜: ์œ„ํ‚ค๋ถ์Šค)

์œ„์˜ ๋‹ค์ด์–ด๊ทธ๋žจ์„ ๋ณด๋ฉด, ์˜ํ™”์—๋Š” ํ• ์ธ์ •์ฑ…์ด ์žˆ๊ณ  ์ •์ฑ…์—๋Š” ์กฐ๊ฑด๋“ค์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์ถ”์ƒํ™”๋œ ๊ฐ์ฒด๋“ค ๊ฐ„์˜ ํ˜‘๋ ฅ์„ ํ†ตํ•ด์„œ ์ง๊ด€์ ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ƒˆ๋กœ์šด ํ• ์ธ ์ •์ฑ… ํ˜น์€ ์กฐ๊ฑด์ด ์ถ”๊ฐ€๋˜์–ด๋„ ๊ธฐ์กด์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š” ์œ ์—ฐํ•จ ๋˜ํ•œ ์ถ”์ƒํ™”์˜ ์žฅ์ ์ž…๋‹ˆ๋‹ค.

Abstract Base Class(ABC)

์ด๋Ÿฌํ•œ ์ถ”์ƒํ™”์˜ ์žฅ์ ๋“ค์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ PEP3119 ๋ฅผ ํ†ตํ•ด์„œ, Python ์ฐฝ์‹œ์ž์ธ ๊ท€๋„ ๋ฐ˜ ๋กœ์„ฌ์ด ์ œ์•ˆํ•˜์˜€์Šต๋‹ˆ๋‹ค.

PEP3119

Rationale ๋ถ€๋ถ„์„ ๋ณด๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด OOP์—์„œ ๊ฐ์ฒด์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ๋ฐฉ์‹์—๋Š” 2๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค๊ณ  ์ด์•ผ๊ธฐํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋กœ Invocation (ํ˜ธ์ถœ) ๊ทธ๋ฆฌ๊ณ  Inspection (๊ฒ€์‚ฌ) ์ž…๋‹ˆ๋‹ค.

ํ˜ธ์ถœ์€ OOP ์—์„œ ์ƒ์†, ํ•ฉ์„ฑ๋“ฑ์˜ ๋‹คํ˜•์„ฑ์— ์˜ํ•ด์„œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Python์—์„œ๋Š” mro() ๋ฅผ ํ†ตํ•ด์„œ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ˆœ์„œ๋ฅผ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ˜ธ์ถœ์„ ํ†ตํ•ด์„œ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ด์ง€๋Š” ๊ฒƒ์ด์ฃ .

class A:
    pass

class B(A):
    pass

class C(A):
    pass

class D(B, C):
    pass

>>> D.__mro__
(__main__.D, __main__.B, __main__.C, __main__.A, object)

๋‹ค์Œ์œผ๋กœ ๊ฒ€์‚ฌ๋Š” ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์–ด๋–ค ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ธ์ง€ ํ˜น์€ ์„œ๋ธŒํด๋ž˜์Šค์ธ์ง€, ์™ธ๋ถ€์—์„œ ํ˜ธ์ถœํ•˜๋Š” ์†์„ฑ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€, ๋˜๋Š” ๋งค์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜์—ฌ ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์ฃผ์–ด์ง„ ๋ฉ”์‹œ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

Code

abc ๋ชจ๋“ˆ์ด ์ง€์›ํ•˜๋Š” ๊ธฐ๋Šฅ์€ ํฌ๊ฒŒ 2๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ๊ตฌํ˜„์„ ๊ฐ•์ œํ•˜๋Š” @abstractmethod
  2. ์ƒ์†์„ ๋ฐ›์€ ๊ฐ์ฒด๋ฅผ ์„œ๋ธŒํด๋ž˜์Šค๋กœ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ

๊ทธ๋ ‡๋‹ค๋ฉด ์ฐจ๋ก€๋Œ€๋กœ 2๊ฐ€์ง€ ๊ธฐ๋Šฅ์ด ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

@abstractmethod

# https://github.com/python/cpython/blob/3.9/Lib/abc.py#L7

def abstractmethod(funcobj):
    """A decorator indicating abstract methods.
    ...
    Usage:
        class C(metaclass=ABCMeta):
            @abstractmethod
            def my_abstract_method(self, ...):
                ...
    """
    funcobj.__isabstractmethod__ = True
    return funcobj

์œ„์™€ ๊ฐ™์ด ๋ฉ”์„œ๋“œ์— abstract ์—ฌ๋ถ€๋งŒ ์ฒดํฌํ•ด์ฃผ๊ณ , ์ด ํ”Œ๋ž˜๊ทธ๋ฅผ ์ด์šฉํ•ด์„œ ๋™์ž‘ํ•˜๊ฒŒ ๋˜๋Š” ๋กœ์ง๋“ค์€ Usage์— ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, ABCMeta ์— ๊ตฌํ˜„์ด ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์ด์–ด์„œ ๋‹ค์Œ ์ฝ”๋“œ๋„ ํ•œ ๋ฒˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

# https://github.com/python/cpython/blob/3.9/Lib/_py_abc.py#L14

class ABCMeta(type):
   """Metaclass for defining Abstract Base Classes (ABCs).
   ...
   """

    _abc_invalidation_counter = 0

    def __new__(mcls, name, bases, namespace, /, **kwargs):
        cls = super().__new__(mcls, name, bases, namespace, **kwargs)
        # Compute set of abstract method names
        abstracts = {name
                     for name, value in namespace.items()
                     if getattr(value, "__isabstractmethod__", False)}
        for base in bases:
            for name in getattr(base, "__abstractmethods__", set()):
                value = getattr(cls, name, None)
                if getattr(value, "__isabstractmethod__", False):
                    abstracts.add(name)
        cls.__abstractmethods__ = frozenset(abstracts)
        ...

__new__ ๋Š” Python์˜ built-in function ์ค‘์— ํ•˜๋‚˜๋กœ์„œ, ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ์˜ ํ•ด๋‹น ๋กœ์ง๋“ค์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ถ€๋ถ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด __isabstractmethod__ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•ด์„œ abstracts ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๋‚ด๋ถ€์—์„œ ๋”ฐ๋กœ ์ด abstracts ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์ง€๋Š” ์•Š๋Š” ๊ฒƒ ๊ฐ™์•„๋ณด์˜€๋Š”๋ฐ, ์ด ๋ถ€๋ถ„์€ typeobject.c ์ฝ”๋“œ์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

// https://github.com/python/cpython/blob/3.9/Objects/typeobject.c#L3863

static PyObject *
object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    ...
    abstract_methods = type_abstractmethods(type, NULL);
    ...  // ์ฒดํฌ ํ›„ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜์ง€ ์•Š์œผ๋ฉด
    PyErr_Format(PyExc_TypeError,
                     "Can't instantiate abstract class %s "
                     "with abstract method%s %U",
                     type->tp_name,
                     method_count > 1 ? "s" : "",
                     joined);

# ์˜ˆ์‹œ์ฝ”๋“œ

from abc import ABCMeta

class A(metaclass=ABCMeta):

    def __init__(self):
        pass

    @abstractmethod
    def test(self):
        pass

class B(A):

    def __init__(self):
        pass    

>>> b = B()
TypeError: Can't instantiate abstract class B with abstract methods test

์˜ˆ๋ฅผ ๋“ค๋ฉด, abstractmethod ์ธ test ๋Š” ๊ตฌํ˜„์„ ํ•ด์•ผํ•˜๋Š”๋ฐ, B ํด๋ž˜์Šค ์•ˆ์—์„œ ๊ตฌํ˜„์ด ๋˜์–ด์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๋ฉ”์‹œ์ง€๊ฐ€ ๋œจ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์ฃ .

subclass

์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ๊ตฌํ˜„ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ, ์ด ํด๋ž˜์Šค๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์˜ ์„œ๋ธŒํด๋ž˜์Šค๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
์„œ๋ธŒํด๋ž˜์Šค ํŒ๋‹จ ๋˜ํ•œ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ABCMeta ์˜ __new__ ๋ถ€๋ถ„์œผ๋กœ ๋Œ์•„๊ฐ€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

# https://github.com/python/cpython/blob/3.9/Lib/_py_abc.py#L48
 
  _abc_invalidation_counter = 0

  def __new__(...):
      ...
      # Set up inheritance registry
      cls._abc_registry = WeakSet()
      cls._abc_cache = WeakSet()
      cls._abc_negative_cache = WeakSet()
      cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
      return cls

cls ์ฆ‰, ํด๋ž˜์Šค์˜ ์†์„ฑ์œผ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ๊ฐ’๋“ค์€ ๋ชจ๋‘ ์„œ๋ธŒํด๋ž˜์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ์—ญํ• ์„ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • cls._abc_registry : register ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์„œ๋ธŒํด๋ž˜์Šค๋ฅผ ๋“ฑ๋ก
  • cls._abc_cache : ์„œ๋ธŒํด๋ž˜์Šค์ผ ๊ฒฝ์šฐ ์บ์‹ฑํ•˜๋Š” ์šฉ๋„
  • cls._abc_negative_cache : ์„œ๋ธŒํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ๋ฅผ ์บ์‹ฑํ•˜๋Š” ์šฉ๋„

์ถ”๊ฐ€์ ์œผ๋กœ ์‚ดํŽด๋ณผ ๊ฒƒ์€ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” WeekSet ์ž…๋‹ˆ๋‹ค. ์•ฝํ•œ ์ฐธ์กฐ๋ฅผ ์ง€์›ํ•˜๋Š” weekref ๋ชจ๋“ˆ์„ ํ†ตํ•ด์„œ ๊ตฌ์„ฑ๋œ Set ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์•ฝํ•œ ์ฐธ์กฐ๋ž€ ๋ฌด์—‡์ด๊ณ , ์—ฌ๊ธฐ์„œ ์•ฝํ•œ ์ฐธ์กฐ๊ฐ€ ์™œ ์‚ฌ์šฉ๋˜์—ˆ์„๊นŒ์š”?

ํ”ํžˆ ๊ฐ•ํ•œ ์ฐธ์กฐ (Strong Reference)์™€ ์•ฝํ•œ ์ฐธ์กฐ (Weak Reference)๋กœ ๋‚˜๋ˆ„์–ด์„œ ๋ณผ ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด๋Š” GC(Garbage Collection)์— ์˜ํ•ด์„œ ์‚ฌ๋ผ์ง€๋Š๋ƒ ๋งˆ๋Š๋ƒ์˜ ์ฐจ์ด๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ•ํ•œ ์ฐธ์กฐ์˜ ๊ฒฝ์šฐ๋Š” ์ฐธ์กฐ์ˆ˜(reference count)๊ฐ€ 0์ด ๋˜๊ฑฐ๋‚˜ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ•ด์ œ๋  ๋•Œ ์ œ๊ฑฐ๋˜๊ณ , ์•ฝํ•œ ์ฐธ์กฐ์˜ ๊ฒฝ์šฐ์—๋Š” ์ฐธ์กฐ์ˆ˜๋ฅผ ์˜ฌ๋ฆฌ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— GC์— ์˜ํ•ด์„œ ๋งค๋ฒˆ ์ œ๊ฑฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ์‰ฝ๊ฒŒ ์ œ๊ฑฐ๋  ์ˆ˜ ์žˆ๋Š” ์„ฑ์งˆ์€ Cache์— ์‚ฌ์šฉ์ด ๋ฉ๋‹ˆ๋‹ค. Cache๋Š” ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ณ , ์ œ๊ฑฐ๊ฐ€ ๋˜๋”๋ผ๋„ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ถ”๊ฐ€์ ์œผ๋กœ negative cache ์˜ ๊ฒฝ์šฐ, DNS์—์„œ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ˜ธ์ŠคํŠธ์— ๋Œ€ํ•œ ์š”์ฒญ์„ ์บ์‹ฑํ•˜์—ฌ ๋น ๋ฅด๊ฒŒ ๋‹ต์„ ํ•ด์ฃผ๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์„œ๋ธŒํด๋ž˜์Šค ์ฒดํฌ ๋˜ํ•œ ๋‹ค์–‘ํ•œ ํด๋ž˜์Šค์™€ ๋น„๊ต๋ฅผ ํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, negative cache๊นŒ์ง€ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ์ดํ•ด๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋น„์Šทํ•œ ์ƒํ™ฉ์—์„œ ์จ๋จน์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.

# https://github.com/python/cpython/blob/3.9/Lib/_py_abc.py#L54

def register(cls, subclass):
    """Register a virtual subclass of an ABC."""

# https://github.com/python/cpython/blob/3.9/Lib/_py_abc.py#L108

def __subclasscheck__(cls, subclass):
    """Override for issubclass(subclass, cls)."""

๋งˆ์ง€๋ง‰์œผ๋กœ ์„œ๋ธŒํด๋ž˜์Šค ๊ด€๋ จํ•ด์„œ๋Š” ์ฃผ์š” ๋ฉ”์„œ๋“œ์ธ ๋“ฑ๋ก(register)๊ณผ ์ฒดํฌ์ •๋„(__subclasscheck__)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚ด๋ถ€์˜ ์ฝ”๋“œ๋Š” ๋‹จ์ˆœํ•œ ํŽธ์ด๋ผ์„œ ๊ตณ์ด ๋‹ค๋ฃจ์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

collections.abc

ํ”ํžˆ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” collections ์—์„œ๋„ ์ด abc ์ด ์ ์šฉ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ collections.abc ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ๊ธฐ๋ณธ์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์— ๋Œ€ํ•œ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ์— ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

images

collection.abc์˜ ๋‹ค์ด์–ด๊ทธ๋žจ (์ถœ์ฒ˜: Issue Tracker – Python.org)

์œ„ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ํ•ด๋‹น ํด๋ž˜์Šค์— ๋งž์ถฐ์„œ ๋ฉ”์„œ๋“œ๋“ค์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ต‰์žฅํžˆ ์ž์ฃผ ์“ฐ์ด๋Š”, ๊ธฐ๋ณธ์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐํ˜•์˜ ์ถ”์ƒ ๋ฒ ์ด์Šค ํด๋ž˜์Šค๋“ค์ด๋ฏ€๋กœ, ์ง๊ด€์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋ผ๊ณ  ๊ธฐ๋Œ€๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์— ๋Œ€ํ•œ ์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•˜๋ฉด Metaclass๊ฐ€ ๋ฏน์Šค์ธ์œผ๋กœ ์‚ฌ์šฉ๋˜๊ณ  , ์„œ๋ธŒํด๋ž˜์Šค ์ฒดํฌ์šฉ์œผ๋กœ __subclasshook__ ์—์„œ ๋ฉ”์„œ๋“œ๋“ค์„ ํ™•์ธํ•˜๋„๋ก ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

๋์œผ๋กœ

Python์—์„œ ์ง€์›ํ•˜๋Š” ์ถ”์ƒํ™” ๋ชจ๋“ˆ์ธ abc์— ๋Œ€ํ•ด์„œ ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์กฐ๊ธˆ ๋” Pythonic ํ•œ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ์œ„์˜ collections.abc ์˜ ๊ธฐ๋ณธ ์ถ”์ƒํ™” ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋“ค์„ ๊ตฌํ˜„ํ•ด์„œ ์‚ฌ์šฉํ•ด๋ณด๋Š” ๊ฒƒ์ด ์–ด๋–จ๊นŒ์š”?

References

Categories
Misc

Book recommendations for tensorflowjs ?

Hey, I am interested in specifically learning tensorflow for JavaScript. I have learned the basics already and made some simple things like a model that predicts where crypto prices are going, but I keep running into walls trying to make things more complex.

I understand that all tensorflow works generally the same, but it would be cool if the book was JS focused (not a requirement). I would also appreciate a book that didn’t just throw a ton of formulas at you and instead took time to explain them. I have only taken up to calculus in college math so far. Thanks.

submitted by /u/grandcaravan2005
[visit reddit] [comments]

Categories
Misc

Is there any way to install TF in a Docker container on an M1 Mac?

Iโ€™m trying to install Tensorflow 2.4.1 in a Docker container running Ubuntu 20.04 on my M1 MacBook Pro. I keep getting an error that says qemu: uncaught target signal 6 (Aborted) – core dumped

Iโ€™ve seen that this is common issue. I can get it to install and run fine on my base machine, itโ€™s just when installing in the container that I have an issue.

Is there any way to install this properly? Thanks in advance!

submitted by /u/musicman0326
[visit reddit] [comments]

Categories
Misc

Has anyone had any success using Fiverr to do a tensorflow model for image recognition?

submitted by /u/NickLRealtor
[visit reddit] [comments]

Categories
Misc

Set TensorFlow2 tensor to zero based on condition

I have a tensor in TensorFlow 2.5 where the goal is to set the smallest sum of magnitude based on p% of ‘slices’ to zeros. The sample code to achieve this is as follows:

# create example tensor- input_shape = (1, 4, 4, 6) y = tf.random.normal(input_shape) 

The goal is to find and remove the smallest (say) p = 20% of the values from the last index ‘6’ based on the sum of their magnitudes. floor(20% of 6) = 1, i.e., after removing 20% of smallest magnitude summation of values, 5 of the 4×4 ‘slices’ should have non-zero values while the 6th slice should be equal to zero.

 # Sum magnitude values- filter_sum = tf.math.reduce_sum(input_tensor = tf.math.abs(y), axis = [1, 2], keepdims = False) filter_sum.shape # TensorShape([1, 6]) indices = tf.argsort(filter_sum) indices.shape # TensorShape([1, 6]) indices.numpy() # array([[0, 1, 4, 2, 5, 3]]) # 20% of 6 values = 1 math.floor(0.2 * 6) # 1 

In this example, the smallest 4×4 ‘slice’ in ‘y’ is the first slice as computed by ‘indices’. How do I proceed?

โ€‹

Thanks

submitted by /u/grid_world
[visit reddit] [comments]

Categories
Misc

I have a trained handwritten character recognition model, is there an OCR I can feed it to?

Sorry if my question is misleading. I have built and trained a model which can determine what character is the input image (the image is a handwritten letter). I’d like to do not only 1 character, but let’s say a word or a text. Do I have to write my own code which segments the input image of a text/word into characters? If so where should I begin? Or is there a working segmenting model which I could just feed my trained model into then it would do the work?

Oh and the trained model consists of old latin handwritten characters.

I’m really new to this topic so any help would be appreciated! Thank you!

submitted by /u/punkw_
[visit reddit] [comments]