Eager_executionμ TF 2.0μμ κΈ°λ³Έμ μΌλ‘ νμ±νλμ΄ μμ§λ§ .numpy()λ₯Ό μ¬μ©νλ λμ μ€λ₯κ° λ°μν©λλ€.
TF 1.0μ λν νΈνμ± λͺ¨λμμ μ½λλ₯Ό μ¬μ©νμ§ μλλ€λ μ μ μ μνμμμ€.
νΉκΈ = [[[ 0, 0, 0],
[4, 71, 141],
[ 0, 0, 0]],
[[ 83, 25, 85],
[ 90, 190, 143],
[ 4, 141, 49]],
[[ 0, 0, 0],
[ 4, 71, 49],
[ 0, 0, 0]]]
expt = tf.convert_to_tensor(expt)
μμ κ° = ext.numpy()
AttributeError: 'Tensor' κ°μ²΄μ 'numpy' μμ±μ΄ μμ΅λλ€.
TENSORFLOW 2.0μ CPU ν μ€νΈ λ²μ .
@Mainak431 λ¬Έμ λ₯Ό ν΄κ²°νμ ¨λμ?
μ.
λΉμ μ ν΄κ²°μ± μ 무μμ΄μμ΅λκΉ?
.Numpyλ μ΄λ§ λͺ¨λμμλ§ μ§μλ©λλ€. κ·Έλν λͺ¨λμΈ κ²½μ° μ§μλμ§ μμ΅λλ€. λΉμ μ΄ μ΄λ§ λͺ¨λμ μλμ§ νμΈνκΈ° μν΄. νμΈμ, tf.eagerly(). true λλ falseλ₯Ό λ°νν©λλ€. κ·Έλν λͺ¨λμμλ μΈμ μμ evalμ μ¬μ©νμ¬ numpy λ°°μ΄μ ν μ κ°μ κ°μ ΈμμΌ ν©λλ€.
@Mainak431
λͺ¨λ 'tensorflow'μλ 'μ΄μ¬ν' μμ±μ΄ μμ΅λλ€.
λͺ¨λ 'tensorflow.compat.v1'μλ 'μ΄μ¬ν' μμ±μ΄ μμ΅λλ€.
tensorflow-gpu==2.0.0-alpha0 ν¨ν€μ§λ₯Ό μ¬μ©νκ³ μμ΅λλ€.
λ΄ ν
μ μ€ νλμ .numpy() 'Tensor' κ°μ²΄μ 'numpy' μμ±μ΄ μμ΅λλ€.
tf.eagerly()λ 'tensorflow' λͺ¨λμ 'μ΄μ¬ν' μμ±μ΄ μμμ μ 곡ν©λλ€.
tf.eagerly() λμ tf.executing_eagerly()κ° λ΄κ° μ΄λ§ λͺ¨λμ μλμ§ νμΈνλ λ° λμμ΄ λμμ΅λλ€.
λꡬλ μ§ μ΄μ λν ν΄κ²°μ± μ μ°Ύμμ΅λκΉ?
λμΌν λ¬Έμ κ° μ¬κΈ°μ μμ΅λλ€ ... μ΄λ§ λͺ¨λμμ keras λͺ¨λΈμ νΈμΆνκ³ OpenAI Gymμ λ¬Έμ λ₯Ό μΌμΌν€λ EagerTensorκ° μλ Tensorλ₯Ό μ»μ΅λλ€.
μ΄ tf.enable_eager_execution()μ μ€νν λ€μ tf.executing_eagerly()λ₯Ό μλνλ©΄ Trueλ₯Ό μ 곡ν΄μΌ ν©λλ€. κ·Έλ° λ€μ something.numpy()λ₯Ό μ¬μ©νμ¬ κ°μ λ³Ό μ μμ΅λλ€.
@AkashNagaraj λ°©κΈ μ½λκ° μ΄μ¬ν μ€νλλ λ¬Έμ λ₯Ό μ κΈ°νμ§λ§(TF 2.0μ΄λ―λ‘ μ€νν΄μΌ ν¨) "numpy λλ½" λ¬Έμ κ° μμ΅λλ€. ν λ² μ΄ν΄λ³΄μ€λμ? https://github.com/tensorflow/tensorflow/issues/32842
κ°μ¬ ν΄μ!
λλ κ°μ λ¬Έμ κ° μμλ€. @tf.function λ΄μμ .numpy()λ₯Ό μ¬μ©νλ €κ³ νμ΅λλ€. λ΄κ° μ΄ν΄νλ ν tf.functionμ μ±λ₯ λͺ©μ μΌλ‘ μ΄μ¬ν μ€νλμ§ μμ΅λλ€. @tf.function λ°μ½λ μ΄ν°λ₯Ό μ κ±°νλ©΄ .numpy()κ° μλν©λλ€.
λλ κ°μ λ¬Έμ κ° μμλ€. @tf.function λ΄μμ .numpy()λ₯Ό μ¬μ©νλ €κ³ νμ΅λλ€. λ΄κ° μ΄ν΄νλ ν tf.functionμ μ±λ₯ λͺ©μ μΌλ‘ μ΄μ¬ν μ€νλμ§ μμ΅λλ€. @tf.function λ°μ½λ μ΄ν°λ₯Ό μ κ±°νλ©΄ .numpy()κ° μλν©λλ€.
μ΄κ²μ λλ₯Ό μν΄ μλν©λλ€. @tf.functionμ μ 체 κΈ°λ₯μ κ·Έλν λͺ¨λλ‘ μ ννμ΅λλ€.
μ΄ μ€λ λμ μΈκΈλ λ¨μΌ μ루μ μ΄ μλ μ΄μ =.=
λ΄ ν¨μμλ λ°μ½λ μ΄ν°κ° μμ§λ§ μ΄μ ν¬μ€ν°μμ μ€λͺ ν λλ‘ .numpy()λ μ¬μ ν μ€ν¨ν©λλ€. λꡬλ μ§ μ΄κ²μ λν ν΄κ²°μ± μ μ°Ύμμ΅λκΉ?
μ€ν¬λ¦½νΈ μλ¨μ tf.compat.v1.enable_eager_execution()μ μ½μ ν ν λ¬Έμ κ° ν΄κ²°λμμμ λ°κ²¬νμ΅λλ€(μ΄μ ν¬μ€ν°μμ λ§ν κ²κ³Ό λ§€μ° μ μ¬νμ§λ§ TF 2.0μμ μλν¨)...
λμΌν λ¬Έμ κ° λ°μνκ³ μμ μΈκΈν μ루μ μ€ μ΄λ κ²λ ν¨κ³Όκ° μμμ΅λλ€.
TF 2.0μ μ¬μ©νκ³ μλλ° λ΄ ν¨μμ λ°μ½λ μ΄ν°κ° μκ³ tf.eagerly()κ° Trueλ₯Ό λ°ννμ§λ§ μ¬μ ν λμΌν AttributeError: 'Tensor' κ°μ²΄μ 'numpy' μμ±μ΄ μμ΅λλ€.
κ°μ λ¬Έμ .
λλ μ΄μ¬ν μ€ννκ³ μμκ³ λ΄ μ¬μ©μ μ μ μμ€ ν¨μμ λ°μ½λ μ΄ν°κ° μλμ§ νμΈνμ΅λλ€. λλ λν μλνμ§ μλ Michaelμ μ루μ λ κ°μ§ μ견μ μλνμ΅λλ€.
μ€λ₯κ° λ°μν©λλ€. AttributeError: 'Tensor' κ°μ²΄μ 'numpy' μμ±μ΄ μμ΅λλ€.
μ΄ μ€λ₯λ λͺ¨λΈ νΌν μ€μ ν μλ₯Ό numpyλ‘ λ³ννλ €κ³ ν λλ§ λνλλ κ²μΌλ‘ λνλ¬μ΅λλ€. κ°μ₯ μ’μ μΆμΈ‘μ λͺ¨μ λ¬Έμ μΈ κ² κ°μ΅λλ€.
μλ₯Ό λ€μ΄ λ€μ ν μλ
tf.Tensor([[1 3] [0 4]], λͺ¨μ=(2, 2), dtype=int64)
.numpy()λ₯Ό μ¬μ©νμ¬ λ³ν κ°λ₯ν©λλ€. κ·Έλ¬λ λΆλ₯ λ¬Έμ μ λν μ¬μ©μ μ§μ λ©νΈλ¦μ ꡬννλ €κ³ ν λ y_true.numpy() λ° y_pred.numpy() λͺ¨λ
AttributeError: 'Tensor' κ°μ²΄μ 'numpy' μμ±μ΄ μμ΅λλ€.
λ€μμ λ yμ ν μμ λλ€.
y_true:
print(y_true): ν
μ("dense_ target:0 ", λͺ¨μ=(μμ, μμ, μμ), dtype=float32)
μΈμ(μ ν(y_true)):
y_pred:
print(y_pred): Tensor("dense/ Identity:0 ", shape=(None, None, 6), dtype=float32)
μΈμ(μ ν(μμ)):
@renatomello μ λ λΉμ κ³Ό κ°μ λ¬Έμ λ₯Ό κ²ͺκ³ μμ΅λλ€. μ λ¬Έμ #35393μ μ΄μμ΅λλ€.
@renatomello μ¬μ©μ μ§μ λ©νΈλ¦μ ꡬννλ €κ³ ν λ λ¬Έμ κ° μ¬μ ν μ§μλ©λλ€. ν΄κ²° λ°©λ²μ μ°Ύμμ΅λκΉ?
@renatomello μ¬μ©μ μ§μ λ©νΈλ¦μ ꡬννλ €κ³ ν λ λ¬Έμ κ° μ¬μ ν μ§μλ©λλ€. ν΄κ²° λ°©λ²μ μ°Ύμμ΅λκΉ?
μλμ λλ μ κ·Έλ¬μ΄μ. λ΄κ° μμ ν μ μλ κ²κ³Ό μ μ¬ν μμ μ μννλ TF/Keras λ°±μλ κΈ°λ₯μ΄ μλμ§ νμΈνλ €κ³ ν©λλ€. κ·Έλ μ§ μμΌλ©΄ λ΄κ° μ§μ λ§λ€μ΄μΌ ν©λλ€.
λ°μ΄ν° μ μ²λ¦¬ λ΄μμ μ κ·μ ν¨μλ₯Ό μ¬μ©ν λ μ΄ μμμ΄ λ°μνμ΅λλ€. νμ΄μ¬ λ‘μ§μ μ¬μ©νλ €λ©΄ λ¬Έμ μ μ΄ StackOverflow μ€λ λμ μΈκΈλ λλ‘ tf.py_function
λ₯Ό μ¬μ©ν΄μΌ ν©λλ€ (μ°Έκ³ tf.py_func()
λ μ΄μ tf.py_function()
).
μΌλ¨ λ΄ μ½λλ₯Ό λ€μμμ λ³κ²½νμ΅λλ€.
data = fnames.map(process_path)
μκ²
data = fnames.map(lambda x: tf.py_function(process_path, [x], [tf.string]))
μ½λκ° μ¬λ°λ₯΄κ² μ€νλμμ΅λλ€.
λ§€μ° μ¬μ©μ μ μλ split
μ¬μ©νμ¬ TF2.1μμ TextVectorization
λ μ΄μ΄λ₯Ό μλνλ λμ μ΄ λ¬Έμ κ° λ°μνμ΅λλ€. λλ₯Ό μν΄ κ·Έκ²μ κ³ μΉ κ²μ dynamic=True
λ₯Ό TextVectorization
κ΅¬μ‘°λ‘ μ λ¬νλ κ²μ΄μμ΅λλ€.
μ΄ μ€λ₯λ λͺ¨λΈ νΌν μ€μ ν μλ₯Ό numpyλ‘ λ³ννλ €κ³ ν λλ§ λνλλ κ²μΌλ‘ λνλ¬μ΅λλ€. κ°μ₯ μ’μ μΆμΈ‘μ λͺ¨μ λ¬Έμ μΈ κ² κ°μ΅λλ€.
μλ₯Ό λ€μ΄ λ€μ ν μλ
tf.Tensor([[1 3] [0 4]], λͺ¨μ=(2, 2), dtype=int64)
.numpy()λ₯Ό μ¬μ©νμ¬ λ³ν κ°λ₯ν©λλ€. κ·Έλ¬λ λΆλ₯ λ¬Έμ μ λν μ¬μ©μ μ§μ λ©νΈλ¦μ ꡬννλ €κ³ ν λ y_true.numpy() λ° y_pred.numpy() λͺ¨λ
AttributeError: 'Tensor' κ°μ²΄μ 'numpy' μμ±μ΄ μμ΅λλ€.
λ€μμ λ yμ ν μμ λλ€.
y_true:
print(y_true): ν μ("dense_ target:0 ", λͺ¨μ=(μμ, μμ, μμ), dtype=float32)
μΈμ(μ ν(y_true)):y_pred:
print(y_pred): Tensor("dense/ Identity:0 ", shape=(None, None, 6), dtype=float32)
μΈμ(μ ν(μμ)):
μ΄μ λ₯Ό λͺ¨λ₯΄μ§λ§ λ€μμ μΆκ°νμ¬ μ΄λ¬ν λ¬Έμ λ₯Ό ν΄κ²°νμ΅λλ€.
experimental_run_tf_function=False
λ΄ λͺ¨λΈμ μ»΄νμΌ κΈ°λ₯μμ.
μ΄ μ€λ₯λ λͺ¨λΈ νΌν μ€μ ν μλ₯Ό numpyλ‘ λ³ννλ €κ³ ν λλ§ λνλλ κ²μΌλ‘ λνλ¬μ΅λλ€. κ°μ₯ μ’μ μΆμΈ‘μ λͺ¨μ λ¬Έμ μΈ κ² κ°μ΅λλ€.
μλ₯Ό λ€μ΄ λ€μ ν μλ
tf.Tensor([[1 3] [0 4]], λͺ¨μ=(2, 2), dtype=int64)
.numpy()λ₯Ό μ¬μ©νμ¬ λ³ν κ°λ₯ν©λλ€. κ·Έλ¬λ λΆλ₯ λ¬Έμ μ λν μ¬μ©μ μ§μ λ©νΈλ¦μ ꡬννλ €κ³ ν λ y_true.numpy() λ° y_pred.numpy() λͺ¨λ
AttributeError: 'Tensor' κ°μ²΄μ 'numpy' μμ±μ΄ μμ΅λλ€.
λ€μμ λ yμ ν μμ λλ€.
y_true:
print(y_true): ν μ("dense_ target:0 ", λͺ¨μ=(μμ, μμ, μμ), dtype=float32)
μΈμ(μ ν(y_true)):
y_pred:
print(y_pred): Tensor("dense/ Identity:0 ", shape=(None, None, 6), dtype=float32)
μΈμ(μ ν(μμ)):μ΄μ λ₯Ό λͺ¨λ₯΄μ§λ§ λ€μμ μΆκ°νμ¬ μ΄λ¬ν λ¬Έμ λ₯Ό ν΄κ²°νμ΅λλ€.
experimental_run_tf_function=False
λ΄ λͺ¨λΈμ μ»΄νμΌ κΈ°λ₯μμ.
λ΄ κ²½μ°μλ λ³ μ°¨μ΄κ° μλ κ² κ°λ€
λ§€μ° μ¬μ©μ μ μλ
split
μ¬μ©νμ¬ TF2.1μμTextVectorization
λ μ΄μ΄λ₯Ό μλνλ λμ μ΄ λ¬Έμ κ° λ°μνμ΅λλ€. λλ₯Ό μν΄ κ·Έκ²μ κ³ μΉ κ²μdynamic=True
λ₯ΌTextVectorization
κ΅¬μ‘°λ‘ μ λ¬νλ κ²μ΄μμ΅λλ€.
μ νν μ΄λ»κ² νμ ¨λμ?
λ¨Όμ λ€μκ³Ό κ°μ΄ Sessionμ λ±λ‘ν΄μΌ ν©λλ€.
μΈμ
=tf.μΈμ
()
κ·Έλ° λ€μ .. μ¬μ© :
μμ κ° = expt.eval(μΈμ
=μΈμ
)
κ²°κ³Όλ λ€μκ³Ό κ°μμΌ ν©λλ€.
μΈμ(μμ κ°)
μμ[41]:
λ°°μ΄([[[ 0, 0, 0],
[4, 71, 141],
[ 0, 0, 0]]]])
κ°μ¬νλ€
λ§€μ° μ¬μ©μ μ μλ
split
μ¬μ©νμ¬ TF2.1μμTextVectorization
λ μ΄μ΄λ₯Ό μλνλ λμ μ΄ λ¬Έμ κ° λ°μνμ΅λλ€. λλ₯Ό μν΄ κ·Έκ²μ κ³ μΉ κ²μdynamic=True
λ₯ΌTextVectorization
κ΅¬μ‘°λ‘ μ λ¬νλ κ²μ΄μμ΅λλ€.μ νν μ΄λ»κ² νμ ¨λμ?
μ΄μ κ°μ΄. λλ μ¬μ ν μΌλ°μ μΌλ‘ ML/TFλ₯Ό λ°°μ°κ³ μμΌλ―λ‘ μ΄κ²μ΄ μλͺ»λ κ²μΌ μ μμ΅λλ€. κ·Έκ²μ΄ 'κ·Έλ₯ μλ'ν λκΉμ§ κ·Έκ²μ λͺ¨λ νλ§λ²μ λλ€.
vectorize_layer = tf.keras.layers.experimental.preprocessing.TextVectorization(
standardize=tf_custom_standardize,
split=tf_custom_split,
max_tokens=len(vocab)+1,
output_mode='int',
dynamic=True
)
μ΄ λ¬Έμ λ μ¬μ ν λμκ² λ¨μ μμ΅λλ€. @tf.function μ£Όμμ μ¬μ©νμ§ μκ³ μ΄μ¬ν μ€ννκ³ μμ΅λλ€. μ§κΈ μ¬μ©νκ³ μλ ν΄κ²° λ°©λ²μ np.array(yourtensor.to_list())μ λλ€.
μ΄ λ¬Έμ λ μ¬μ ν λμκ² λ¨μ μμ΅λλ€. @tf.function μ£Όμμ μ¬μ©νμ§ μκ³ μ΄μ¬ν μ€ννκ³ μμ΅λλ€. μ§κΈ μ¬μ©νκ³ μλ ν΄κ²° λ°©λ²μ np.array(yourtensor.to_list())μ λλ€.
μ΄κ²μ΄ λ΄κ° μ»λ κ²μ
λλ€.
AttributeError: 'Tensor' object has no attribute 'to_list'
tensorflowμ μ½λμ λ°λ₯΄λ©΄ ν
μ κ°μ²΄κ° μ΄λ§ λͺ¨λ tensor(tensorflow.python.framework.ops.EagerTensor)μ μμ λ tesnsorObj.numpy()λ₯Ό νΈμΆν μ μμ΅λλ€.
μ΄ λ°©λ²μ tensorflow/python/framework/ops.pyμμ ꡬνλ©λλ€.
κ·Έλ¬λ ν
μ κ°μ²΄κ° μΌλ° ν
μ(μ: tensorflow.python.framework.ops.Tensorμ μν¨)μΈ κ²½μ° numpy() λ©μλκ° μκ³ κ΅¬ν μ½λλ tensorflow/python/framework/ops.pyμ μμ΅λλ€.
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/framework/ops.py
@tf.functionμμ μ€ν μ€μΈ λͺ¨λ ν
μ κ°μ²΄κ° tensorObj.numpy()λ₯Ό νΈμΆν μ μμμ μλ―Έν©λλ€.
κΈ΄κΈ μ€ν λͺ¨λλ TF2.0μ μ μ°μ±μ μΆκ°νκ³ λ³΅μ‘μ±μ μΆκ°ν©λλ€.
tensorflowμ μ½λμ λ°λ₯΄λ©΄ ν μ κ°μ²΄κ° μ΄λ§ λͺ¨λ tensor(tensorflow.python.framework.ops.EagerTensor)μ μμ λ tesnsorObj.numpy()λ₯Ό νΈμΆν μ μμ΅λλ€.
μ΄ λ°©λ²μ tensorflow/python/framework/ops.pyμμ ꡬνλ©λλ€.
κ·Έλ¬λ ν μ κ°μ²΄κ° μΌλ° ν μ(μ: tensorflow.python.framework.ops.Tensorμ μν¨)μΈ κ²½μ° numpy() λ©μλκ° μκ³ κ΅¬ν μ½λλ tensorflow/python/framework/ops.pyμ μμ΅λλ€.
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/framework/ops.py@tf.functionμμ μ€ν μ€μΈ λͺ¨λ ν μ κ°μ²΄κ° tensorObj.numpy()λ₯Ό νΈμΆν μ μμμ μλ―Έν©λλ€.
κΈ΄κΈ μ€ν λͺ¨λλ TF2.0μ μ μ°μ±μ μΆκ°νκ³ λ³΅μ‘μ±μ μΆκ°ν©λλ€.
μμΈν μ€λͺ κ°μ¬ν©λλ€.
κ·Έλ¬λ μ΄ λ¬Έμ λ μμ λ²μ 2.1.0 λ²μ μμλ ν΄κ²°λμ§ μμ΅λλ€. #38038μμ tf-nightly 2.2.0 λ²μ μμ ν΄κ²°λμλ€κ³ μ§μ νμ΅λλ€.
@renatomello μ λ TFμ μμ ν μ΄λ³΄μμ
λλ€. λ°λΌμ λ€μμ΄ λ¬Έμ λ₯Ό ν΄κ²°νλ λ° λμμ΄ λ μ§ μ λͺ¨λ₯΄κ² μ΅λλ€.
λλ Experiment_run_functions_eagerly(True) λ₯Ό μ¬μ©νμ¬ AttributeError: 'Tensor' object has no attribute 'numpy'
μ€λ₯λ₯Ό ν΄κ²°ν©λλ€.
λλ TF λ²μ μ μ¬μ©ν©λλ€. 2.1(μμΈν λ²μ μ 보λ μλ μ½μ μΆλ ₯ μ°Έμ‘°). μ΄ μ½λλ‘ λ¬΄μμ νλμ§ μ€λͺ
ν μ μμ΅λλ€.
import sys
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
counter=0
class MyDense(layers.Dense):
def __init__(self, units, activation,input_shape):
super().__init__(units=units, activation=activation,input_shape=input_shape)
def call(self, inputs):
global counter
counter += 1
print('\n{}. inputs.numpy()='.format(counter))
if hasattr(inputs,'numpy'):
print('{}'.format(inputs.numpy()))
else:
print('not available.')
return super().call(inputs)
def test(run_eagerly):
print('\n*** *** *** test(run_eagerly={})'.format(run_eagerly))
tf.config.experimental_run_functions_eagerly(run_eagerly)
dim0=256
dim1=24
train = np.arange(dim0*dim1).reshape(dim0,dim1)
label = np.ones(dim0)
model = tf.keras.Sequential()
model.add(MyDense(10,activation='softmax',input_shape=(dim1,)))
model.compile(optimizer=tf.keras.optimizers.SGD(),
loss=tf.keras.losses.SparseCategoricalCrossentropy())
model.fit(train,
label,
batch_size=dim0,
epochs=1)
print("Python version")
print (sys.version)
print("TensorFlow version")
print(tf.__version__)
print('\n\n')
test(False)
test(True)
μλν° Spyderμ μ½μ(Spyder λ²μ 4.0.1)μμ μμ μ½λλ₯Ό μ€νν©λλ€. ν΄λΉ μ½μ μΆλ ₯ μ λ€μκ³Ό κ°μ΅λλ€(
Python version
3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0]
TensorFlow version
2.1.0
*** *** *** test(run_eagerly=False)
1. inputs.numpy()=
not available.
Train on 256 samples
2. inputs.numpy()=
not available.
3. inputs.numpy()=
not available.
256/256 [==============================] - 0s 899us/sample - loss: 6344.8682
*** *** *** test(run_eagerly=True)
4. inputs.numpy()=
not available.
Train on 256 samples
5. inputs.numpy()=
[[2328. 2329. 2330. ... 2349. 2350. 2351.]
[5280. 5281. 5282. ... 5301. 5302. 5303.]
[2208. 2209. 2210. ... 2229. 2230. 2231.]
...
[5160. 5161. 5162. ... 5181. 5182. 5183.]
[ 840. 841. 842. ... 861. 862. 863.]
[6048. 6049. 6050. ... 6069. 6070. 6071.]]
256/256 [==============================] - 0s 132us/sample - loss: 16.1181
μ΄μ test(run_eagerly=False)
μλ μ½μ μΆλ ₯μ μ°Έμ‘°νμμμ€. λͺ¨λ μΈ κ°μ§ MyDense.call() νΈμΆ μνμμ input.numpy()λ₯Ό μ¬μ©ν μ μμ΅λλ€(μ°Έκ³ : λ΄κ° μ¬λ°λ₯΄κ² μ΄ν΄νλ©΄ μ²μ λ κ°μ MyDense.call () νΈμΆμ λͺ¨λΈμ λΉλνκΈ° μν κ²μ΄λ―λ‘ input.numpy()λ₯Ό μ¬μ©ν μ μλ€λ μλ―Έμ
λλ€.) λ°λΌμ μ΄κ²μ μλ―Έκ° μμ΅λλ€.
test(run_eagerly=True)
μλ μ½μ μΆλ ₯ μ°Έμ‘°: λ€μ― λ²μ§Έ MyDense.call()μμ input.numpy()λ₯Ό μ¬μ©ν μ μμ΅λλ€. κ·Έλμ μ΄κ²λ μλ―Έκ° μμ΅λλ€. κ·Έλ¦¬κ³ κ·Έκ²μ μ€λ₯λ₯Ό ν΄κ²°ν©λλ€.
λλ κ°μ λ¬Έμ κ° μμλ€. @tf.function λ΄μμ .numpy()λ₯Ό μ¬μ©νλ €κ³ νμ΅λλ€. λ΄κ° μ΄ν΄νλ ν tf.functionμ μ±λ₯ λͺ©μ μΌλ‘ μ΄μ¬ν μ€νλμ§ μμ΅λλ€. @tf.function λ°μ½λ μ΄ν°λ₯Ό μ κ±°νλ©΄ .numpy()κ° μλν©λλ€.
κ·Έκ²μ λλ₯Ό μν΄ μλν©λλ€.
μ΄ μ€λ λμμ μΈκΈν λͺ¨λ μ루μ μ μλνμ§λ§ κ·Έ μ€ μ΄λ κ²λ μ μκ² ν¨κ³Όκ° μμμ΅λλ€. μ λ 2.2.0-rc2μ μμ΅λλ€.
μ κ²½μ°μλ μ΄μ¬ μ€νμ λ¬Έμ κ° μλ κ² κ°μ΅λλ€. keras.backend.get_value()
νΈμΆν λ λ€μ μ€μμ μ€λ₯κ° λ°μν©λλ€.
if context.executing_eagerly() or isinstance(x, ops.EagerTensor):
return x.numpy()
κ°μ λ¬Έμ κ°μμμ΅λλ€. λͺ¨λΈμμ λ§μΆ€ κΈ°λ₯μ μ€νν λ κΈ°λ³Έμ μΌλ‘ μ¦μ μ€νμ΄ λΉνμ±νλ©λλ€. μ΄μ λμνκΈ° μν΄ model.fitμ μ€ννκΈ° μ μ model.run_eagerly = True
λ₯Ό μ€νν μ μμ΅λλ€.
μ΄κ²μ Tensorflow 2.2μμ λλ₯Ό μν΄ λ§€λ ₯μ²λΌ μλνμ΅λλ€.
model.compile(..., run_eagerly=True) μ¬μ©μ μ§μ λ©νΈλ¦μ λ§λ€ λ μ μκ² ν¨κ³Όμ μ΄μμ΅λλ€.
Kerasλ‘ μμ
νλ λμ λΉμ·ν λ¬Έμ κ° λ°μνμ΅λλ€.
μ΄κ²μ΄ λ€λ₯Έ μ¬λμκ² λμμ΄ λ μ§ λͺ¨λ₯΄κ² μ§λ§ λ€μμ΄ λμμ΄λμμ΅λλ€.
λλ μ¬μ©νκ³ μμλ€:
model = Sequential() μ΄μ μ λ€μμΌλ‘ λ³κ²½νμ΅λλ€.
λͺ¨λΈ = tf.keras.Sequential()
κ·Έλ¦¬κ³ κ·Έκ²μ λλ₯Ό μν΄ μΌνμ΅λλ€.
@Mainak431 λ¬Έμ λ₯Ό λ€μ μ¬μκ² μ΅λκΉ?
Kerasλ‘ μμ νλ λμ λΉμ·ν λ¬Έμ κ° λ°μνμ΅λλ€.
μ΄κ²μ΄ λ€λ₯Έ μ¬λμκ² λμμ΄ λ μ§ λͺ¨λ₯΄κ² μ§λ§ λ€μμ΄ λμμ΄λμμ΅λλ€.
λλ μ¬μ©νκ³ μμλ€:
model = Sequential() μ΄μ μ λ€μμΌλ‘ λ³κ²½νμ΅λλ€.
λͺ¨λΈ = tf.keras.Sequential()
κ·Έλ¦¬κ³ κ·Έκ²μ λλ₯Ό μν΄ μΌνμ΅λλ€.
κ·Έκ²μ μ€μ λ‘ λλ₯Ό μν μ루μ μ΄μμ΅λλ€. tensorflow.keras λ μ΄μ΄ λ±μ΄ μλ kerasμμ μ§μ κ°μ Έμ¨ μΌλΆ μ€λλ μ½λλ₯Ό μ¬μ¬μ©νμ΅λλ€. κ°μ Έμ€κΈ° λ¬Έμ λ³κ²½νλ©΄ μμ± μ€λ₯κ° ν΄κ²°λμμ΅λλ€.
@Blubbaa μ¬κΈ°λ λ§μ°¬κ°μ§ μ λλ€. κ·Έκ²μ λλ₯Ό μν΄ μΌν μ μΌν κ²μ΄μμ΅λλ€.
ν
μμμ κ°μ νμΈνκ³ μΆμλ° tf.placeholder
νΈνμ± λλ¬Έμ Eager λͺ¨λλ₯Ό λΉνμ±νν΄μΌ νλ κ²½μ° μ΄λ»κ² ν΄μΌ νλμ? tf.placeholder
μμ΄ λ μ΄μ΄ μ¬μ΄μ tf.enable_eager_execution
λ₯Ό μΆκ°νλ €κ³ μλνλλ° ValueError: tf.enable_eager_execution must be called at program startup.
μ€λ₯κ° λ°μ
λ€μ μ½λκ° μλνμ΅λλ€.
def parse_str(str_tensor):
raw_string = str_tensor.numpy().decode("utf-8")
# play with raw string
raw_string = 'AAA'+raw_string
return raw_string
ꡬ문 λΆμ κΈ°λ₯ νΈμΆ:
def tf_pre_processing(row):
return tf.py_function(parse_str, [row['context']], [tf.string])
train = t.map(tf_pre_processing).batch(1).take(1)
list(train)
@Mainak431 μλ μ½λκ° μμλλ‘ μλνμ΅λλ€. μ¬κΈ° μμ§κ° μμ΅λλ€.
@tu1258 μ¦μ μ€νμ λΉνμ±νν νμκ° μμ΅λλ€. κ·Έλ₯λ‘ κ°μ Έ tf.compat.v1.placeholder
λμ tf.placeholder
. κ·Έλλ μλνμ§ μμΌλ©΄ λ
립 μ€νν μ½λλ₯Ό 곡μ νμ¬ λ¬Έμ λ₯Ό μ¬ννμμμ€.
μ§λ¬Έμ΄ λ μμΌλ©΄ μλ €μ£ΌμΈμ. κ°μ¬ ν΄μ!
λλ κ°μ λ¬Έμ κ° μμλ€. @tf.function λ΄μμ .numpy()λ₯Ό μ¬μ©νλ €κ³ νμ΅λλ€. λ΄κ° μ΄ν΄νλ ν tf.functionμ μ±λ₯ λͺ©μ μΌλ‘ μ΄μ¬ν μ€νλμ§ μμ΅λλ€. @tf.function λ°μ½λ μ΄ν°λ₯Ό μ κ±°νλ©΄ .numpy()κ° μλν©λλ€.
Tensorflow2.xμ λ€μ μ΅μ
μ΄ λμμ΄ λ μ μλ€κ³ μκ°ν©λλ€.
tf.config.run_functions_eagerly
tf.functionμ νΈμΆνλ μμΉμμ μ¦μ μ€νμ μλν λ€μ tf.functionμ λν μ¦μ μ€νμ λΉνμ±νν μ μμ΅λλ€.
μ λ tensorflowλ₯Ό μ²μ μ¬μ©νμ§λ§ μ΄ νν 리μΌ(Jupyter Notebookμμ)μ λ°λ₯΄λ©΄ https://www.tensorflow.org/tutorials/quickstart/beginner?hl=en μ€λ₯κ° λ°μν©λλ€.
predictions = model(x_train[:1]).numpy()
Tensor object has no attribute 'numpy'
@louisnot μ€λ₯λ₯Ό μ¬νν μ μμ΅λλ€. μ¬κΈ° μμ νμμμ€ . κ°μ¬ ν΄μ!
μ€λ₯κ° λ°μνλ©΄ μμ μ 곡μ ν μ μμ΅λκΉ? κ°μ¬ ν΄μ!
μμ μ μ¬μ©νλ λ° λ¬Έμ κ° μμμ΅λλ€.
tf-nightly
μμ ν΄κ²°λμμΌλ―λ‘ μ΄ λ¬Έμ λ₯Ό λ«μ΅λλ€. λ¬Έμ κ° μ§μλλ©΄ μΈμ λ μ§ λ€μ μ΄μ΄μ£ΌμΈμ. κ°μ¬ ν΄μ!
@bhupendrathore μ€λ₯λ₯Ό μ¬ννκΈ° μν΄ κ°λ¨ν λ 립 μ€νν μ½λλ‘ μ λ¬Έμ λ₯Ό μ΄μ΄ μ£Όμκ² μ΅λκΉ? κ°μ¬ ν΄μ!
κ°μ λ¬Έμ κ° λ°μν©λλ€. pip install tf-nightly
λ₯Ό μ¬μ©νμ¬ μ루μ
μμ μμ νλ©΄ μ€λ₯κ° λ°μν©λλ€.
ERROR: Could not find a version that satisfies the requirement tf-nightly (from versions: none)
ERROR: No matching distribution found for tf-nightly
@jvishnuvardhan
@liangzelang λ λ€λ₯Έ μλ‘μ΄ λ¬Έμ λ₯Ό μ΄μμ΅λλ€. κ·Έκ³³μμ ν΄κ²°ν΄λλ¦¬κ² μ΅λλ€. κ°μ¬ ν΄μ
κ°μ₯ μ μ©ν λκΈ
λλ κ°μ λ¬Έμ κ° μμλ€. @tf.function λ΄μμ .numpy()λ₯Ό μ¬μ©νλ €κ³ νμ΅λλ€. λ΄κ° μ΄ν΄νλ ν tf.functionμ μ±λ₯ λͺ©μ μΌλ‘ μ΄μ¬ν μ€νλμ§ μμ΅λλ€. @tf.function λ°μ½λ μ΄ν°λ₯Ό μ κ±°νλ©΄ .numpy()κ° μλν©λλ€.