我对此有误:
C:\ Users \ Akash \ PycharmProjects \ TensorFlow_lessons \ venv \ Scripts \ python.exe C:/Users/Akash/PycharmProjects/TensorFlow_lessons/TenFlow_01.py 2018-07-06 16:15:56.929521:I T:\ src \ github \ tensorflow \ tensorflow \ core \ platform \ cpu_feature_guard.cc:140]您的CPU支持该TensorFlow二进制文件未编译为使用的指令:AVX2 追溯(最近一次通话): 文件“ C:/Users/Akash/PycharmProjects/TensorFlow_lessons/TenFlow_01.py”,第45行,在 feed_dict = {输入:training_data ['输入'],目标:training_data ['目标']}) 正在运行的文件“ C:\ Users \ Akash \ PycharmProjects \ TensorFlow_lessons \ venv \ lib \ site-packages \ tensorflow \ python \ client \ session.py”,行900 run_metadata_ptr) _run中的文件“ C:\ Users \ Akash \ PycharmProjects \ TensorFlow_lessons \ venv \ lib \ site-packages \ tensorflow \ python \ client \ session.py”,行1111 str(subfeed_t.get_shape()))) ValueError:无法为张量为'(?,2)'的张量'Placeholder:0'输入形状(2,1000,1)的值
以退出代码1完成的过程
我的代码:
import numpy as np
import tensorflow as tf
"""Data generation"""
obs = 1000
xs = np.random.uniform(-10, 10, (obs, 1))
zs = np.random.uniform(-10, 10, (obs, 1))
generated_inputs = np.stack((xs, zs))
noise = np.random.uniform(-1, 1, (obs, 1))
generated_targets = 2 * xs - 3 * zs + 5 + noise
np.savez('TF_Intro', inputs=generated_inputs, targets=generated_targets)
# solving with tensorflow
input_size = 2
output_size = 1
# outlining the model
inputs = tf.placeholder('float', [None, input_size]) # feeds data to TF_intro file's input column
targets = tf.placeholder('float', [None, output_size]) # same as above to output column
weights = tf.Variable(tf.random_uniform([input_size, output_size], minval=-0.1, maxval=0.1))
biases = tf.Variable(tf.random_uniform([output_size], minval=-0.1, maxval=0.1))
outputs = tf.matmul(inputs, weights) + biases # matmul is same concpt like dotproduct but its for tensors
"""Choosing objective function and optimization methods"""
mean_loss = tf.losses.mean_squared_error(labels=targets, predictions=outputs) / 2.
optimize = tf.train.GradientDescentOptimizer(learning_rate=0.02).minimize(mean_loss)
"""prepare for execute"""
sess = tf.InteractiveSession()
"""initialisation of variables"""
initializer = tf.global_variables_initializer() # initialises all tensor objects marked as variables
sess.run(initializer)
"""Load training data"""
training_data = np.load('TF_Intro.npz')
"""learning"""
for e in range(100):
_, curr_loss = sess.run([optimize, mean_loss],
feed_dict={inputs: training_data['inputs'],
targets: training_data['targets']})
print(curr_loss)
答案 0 :(得分:1)
当您遇到与张量形状不匹配有关的错误时,请仔细阅读错误消息:
ValueError:无法为形状为((?,2)'的张量'占位符:0'输入形状(2,1000,1)的值
如果您稍微调试一下代码,您将立即注意到错误是由于training_data['inputs']
的形状为(2, 1000, 1)
而inputs
的形状为(?, 2)
。鉴于它们具有不同的形状,因此您无法为training_data['inputs']
“喂入” inputs
。
此问题的解决方案取决于您实际想要做什么。
如果要“连接” xs
和zs
,则需要generated_inputs = np.stack((xs, zs))
而不是使用generated_inputs = tf.concat([xs, zs], 1)
。在这种情况下,generated_inputs
的形状为(1000, 2)
,因此可以将其作为inputs
(形状为(?, 2)
)进给。
但是,如果将generated_inputs = np.stack((xs, zs))
替换为generated_inputs = tf.concat([xs, zs], 1)
,则会出现另一个错误:
_pickle.PicklingError:无法腌制:内置的属性查找SwigPyObject失败
这可能是由于您用np.savez
保存的数组/变量必须具有相同的维数(或者也许是因为我们将尝试保存矩阵?)。您可以解决此问题,例如通过分别保存xs
和zs
。因此,您可能有np.savez('TF_Intro', inputs = generated_inputs, targets=generated_targets)
而不是np.savez('TF_Intro', xs=xs, zs=zs, targets=generated_targets)
。然后,在执行run
方法(在会话中)之前,可以串联xs
和zs
。例如,使用以下代码
...
for e in range(100):
xs = training_data['xs']
zs = training_data['zs']
X = np.concatenate((xs, zs), axis=1)
T = training_data['targets']
_, curr_loss = sess.run([optimize, mean_loss],
feed_dict={inputs: X,
targets: T})
print(curr_loss)
注意:如果执行我建议的更改,您的代码将编译并执行而不会出错,但是我不知道这是否是您希望通过代码实现的目标,我不会不想停留在上面。
顺便说一句,为什么要生成输入和目标,将其存储,然后从文件中读取它们?为什么不直接使用输入和目标(在这种情况下)呢?