因此,假设我有x_train和y_train,它们是数组,并且该数组的每个元素都是一个数据点(以数组形式)(所以x_train的形式为x_train [i] [j])。所以x_train [0]代表训练集中的第一个数据点(以数组形式),并假设我想创建一个简单的回归
所以我对此进行了编码
input = tf.placeholder(tf.float32, shape=[len(data[0]),None])
target = tf.placeholder(tf.flaot32, shape=[len(data[0]),None])
network = tf.layers.Dense(10, tf.keras.activations.relu)(input)
network = tf.layers.BatchNormalization()(network)
network = tf.layers.Dense(10,tf.keras.activations.relu)(network)
network = tf.layers.BatchNormalization()(network)
network = tf.layers.Dense(10,tf.keras.activations.linear)(network)
cost = tf.reduce_mean((target - network)**2)
optimizer = tf.train.AdamOptimizer().minimize(cost)
with tf.Session() as sess:
for epoch in range(1000):
_, val = sess.run([optimizer,cost], feed_dict={input: x_train, target: y_train})
print(val)
但这是正确的吗?我不确定占位符的尺寸是否匹配。当我尝试运行此代码时, 我收到错误消息
ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`.
所以我试图将尺寸尺寸的位置换成占位符,所以 更改后的占位符是 输入= tf.placeholder(tf.float32,shape = [None,len(data [0])]) target = tf.placeholder(tf.float32,shape = [None,len(data [0])])
但是有了这些,我就会收到错误消息
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value dense/bias
[[{{node dense/bias/read}}]]
答案 0 :(得分:1)
我能够通过在优化前先在{{1}的function FirstReverse(str) {
let characterIndex, newstr = "";
for (characterIndex = str.indexOf(str[str.length - 1]); characterIndex >= 0; --characterIndex) {
newstr += str[characterIndex];
}
return newstr;
}
console.log(FirstReverse("hello world"));
和np.expand_dims()
上执行x_train
和y_train
并使用axis=0
初始化batch_norm和网络参数来解决上述问题该模型。
注意:占位符形状的第一个维度中sess.run(tf.global_variable_initializer())
的存在是可以的,因为当batch_size未知时,它允许TensorFlow训练模型(即使对于占位符形状的其他维度也是如此)。该错误是由于输入和占位符尺寸不匹配引起的。您的输入(None
和x_train
可能是一维张量,而占位符要么需要二维张量,要么需要将维矢量重塑为二维。
请找到我下面的实现,以及用于验证实现的y_train
图:
matplotlib
这是费用与时期的图:
另外,要测试网络上的新数据(例如)import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
data = [[1,2,3,4,5,6,7,8,9,10],
[11,12,13,14,15,16,17,18,19,20]]
x_train = data[0]
y_train = data[1]
x_train = np.expand_dims(x_train, 0)
y_train = np.expand_dims(y_train, 0)
input = tf.placeholder(tf.float32, shape=[None, len(data[0])])
target = tf.placeholder(tf.float32, shape=[None, len(data[1])])
network = tf.layers.Dense(10, tf.keras.activations.relu)(input)
network = tf.layers.BatchNormalization()(network)
network = tf.layers.Dense(10,tf.keras.activations.relu)(network)
network = tf.layers.BatchNormalization()(network)
network = tf.layers.Dense(10,tf.keras.activations.linear)(network)
cost = tf.reduce_mean((target - network)**2)
optimizer = tf.train.AdamOptimizer().minimize(cost)
costs = []
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(1000):
_, val = sess.run([optimizer,cost], feed_dict={input: x_train, target: y_train})
costs.append(val)
print(val)
fig, ax = plt.subplots(figsize=(11, 8))
ax.plot(range(1000), costs)
ax.set_title("Costs vs epochs")
ax.set_xlabel("Epoch")
ax.set_ylabel("Avg. val. accuracy")
,可以使用以下代码:
x_test = [[21,22,23,24,25,26,27,28,29,30]]
PS:确保使用与上面创建的相同的Tensorflow会话 y_pred = sess.run(network,feed_dict={input: x_test})
来运行推理(除非您不保存和加载模型检查点)