无法为张量为'(?,2)'的Tensor'Placeholder_24:0'输入形状(25,2,1)的值tensorflow python

时间:2019-02-08 09:38:11

标签: python tensorflow

在这里,我使用张量流设计了一个神经网络。 当我运行它时,它给了我这个错误。我试图对预测值进行逆变换。 谁能帮我解决这个问题? 我的代码:

data_train, data_test = train_test_split(data, test_size=0.2)
scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1))
scaler.fit_transform(data)
data_train =   scaler.fit_transform(data_train)
data_test = scaler.fit_transform(data_test)
X_train = data_train[:,:2]
y_train = data_train[:,3]
X_test = data_test[:,:2]
y_test = data_test[:,3]
n_input = X_train.shape[1]
timesteps = 1
n_classes = 2
n_neurons_1 = 512
n_neurons_2 = 512
n_samples = len(X_train)
net = tf.InteractiveSession()
x = tf.placeholder(tf.float32, [None,n_input])
y = tf.placeholder(tf.float32, [None ])
softmax = 2
weight_initializer =  tf.variance_scaling_initializer(mode="fan_avg", distribution="uniform", scale=softmax)
bias_initializer = tf.zeros_initializer()
W_hidden_1 = tf.Variable(weight_initializer([n_input, n_neurons_1]))
bias_hidden_1 = tf.Variable(bias_initializer([n_neurons_1]))
W_out = tf.Variable(weight_initializer([n_neurons_2,n_classes]))
bias_out = tf.Variable(bias_initializer([n_classes]))
hidden_1 = tf.nn.relu(tf.add(tf.matmul(x, W_hidden_1), bias_hidden_1))
hidden_2 = tf.nn.relu(tf.add(tf.matmul(hidden_1, W_hidden_2), bias_hidden_2))
out = tf.transpose(tf.add(tf.matmul(hidden_2, W_out),bias_out))
mse = tf.reduce_mean(tf.cast(out, tf.float32))
opt = tf.train.AdamOptimizer().minimize(mse)
init = tf.global_variables_initializer()
mse_train = []
mse_test = []
total_len = X_train.shape[0]
#training_epochs = 10
 batch_size = 200
 display_step = 1
 dropout_rate = 0.9
 xrange =[]
 epochs = 10
 for e in range(epochs):
 shuffle_indices =  np.random.permutation(np.arange(len(y_train)))
  X_train = X_train[shuffle_indices]
  y_train = y_train[shuffle_indices]
  for i in range(0, len(y_train) // batch_size):
    start = i * batch_size
    batch_x = X_train[start:start + batch_size]
    batch_y = y_train[start:start + batch_size]
    # Run optimizer with batch
    net.run(opt, feed_dict={x: batch_x, y: batch_y})


    if np.mod(i, 1) == 0:
        # MSE train and test
        mse_train.append(net.run(mse, feed_dict={x: X_train, y: y_train}))
        mse_test.append(net.run(mse, feed_dict={x: X_test, y: y_test}))
        print('Train Error: ' + str(round(100.0 * mse_train[-1], 2)) + ' %')
        print('Test Error: ' + str(round(100.0 * mse_test[-1], 2)) + ' %')
        # Prediction
    pred = net.run(out, feed_dict={x: X_test})
    pred1 = scaler.inverse_transform(pred)
     print(pred1)
     plt.ion()
     fig = plt.figure()
     ax1 = fig.add_subplot(111)
     line1, = ax1.plot(y_test)
     line2, = ax1.plot(y_test * 0.6)
     plt.show()
     line2.set_ydata(pred)
      print(pred)

我有三个输入数据,根据这三个输入数据,我将根据y列预测值。在这里,我上传了我的csv的子集:

enter image description here

1 个答案:

答案 0 :(得分:1)

看起来batch_x的形状为(25,2,1),这意味着它具有多余的尺寸1。您可以将此数组重塑为形状(25,2):

batch_x = numpy.reshape(batch_x, (batch_x.shape[0], batch_x.shape[1]))