tensorflow - 任何新版本的 tf.placeholder?
问题描述
我在使用 时遇到问题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 ]]
推荐阅读
- javascript - 如何异步预加载图像
- javascript - 基于键合并多个对象数组
- php - php中的foreach循环条件执行
- c# - 如何在 C# ChromeDriver 中禁用加载 javascript?
- django - 导出具有反向外键关系的模型的 xlsx 文件,并将该反向外键关系作为单独的列
- node.js - Nodejs express API无法在本地的公共作品中连接
- java - Thymeleaf 模型与 HttpServletRequest
- swift - UITableview 单元格高度在 iOS 14.0 中的滚动表视图上发生变化
- javascript - 如何查看在网页上单击按钮时正在发生的过程?
- vb.net - 使用 vb.net 以编程方式为系统范围的“访问权限”添加或删除用户