我使用tf.placeholder
有问题,因为在新版本的Tensorflow 2.0中已将其删除。
我现在该怎么办才能使用此功能?
您只需直接将数据作为输入应用于层即可。例如:
import tensorflow as tf
import numpy as np
x_train = np.random.normal(size=(3, 2))
astensor = tf.convert_to_tensor(x_train)
logits = tf.keras.layers.Dense(2)(astensor)
print(logits.numpy())
# [[ 0.21247671 1.97068912]
# [-0.17184766 -1.61471399]
# [-0.03291694 -0.71419362]]
上面代码的TF1.x
等效于:
import tensorflow as tf
import numpy as np
input_ = np.random.normal(size=(3, 2))
x = tf.placeholder(tf.float32, shape=(None, 2))
logits = tf.keras.layers.Dense(2)(x)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(logits, feed_dict={x:input_}))
# [[-0.17604277 1.8991518 ]
# [-1.5802367 -0.7124136 ]
# [-0.5170298 3.2034855 ]]