ValueError: Cannot feed value of shape (6165, 5) for Tensor 'Placeholder_1:0', which has shape '(?, 1)'
ValueError: Cannot feed value of shape (6165, 5) for Tensor 'Placeholder_1:0', which has shape '(?, 1)'
> WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
* https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
* https://github.com/tensorflow/addons
If you depend on functionality not listed there, please file an issue.
WARNING:tensorflow:From C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise/TimeSeriespy_RNN.py:74: BasicLSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This class is equivalent as tf.keras.layers.LSTMCell, and will be replaced by that in Tensorflow 2.0.
WARNING:tensorflow:From C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise/TimeSeriespy_RNN.py:75: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.
Instructions for updating:
Please use `keras.layers.RNN(cell)`, which is equivalent to this API
WARNING:tensorflow:From C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\tensorflow\python\ops\tensor_array_ops.py:162: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
Traceback (most recent call last):
File "<ipython-input-1-7716630f4e29>", line 1, in <module>
runfile('C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise/TimeSeriespy_RNN.py', wdir='C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise')
File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise/TimeSeriespy_RNN.py", line 97, in <module>
X: trainX, Y: trainY})
File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
run_metadata_ptr)
File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1128, in _run
str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (6165, 5) for Tensor 'Placeholder_1:0', which has shape '(?, 1)'
我遇到了一个错误,我只是检查了每个变量的维度,它看起来没有任何问题...你能告诉我哪里出了问题以及如何解决吗?
我想做的是天气预报。
输入形状将是 ( xxxx , 5),这里 xxxx 是输入数据的行数,5 是输入的类型,包括平均温度等。
输出形状必须是 (yyyy, 1),因为它的列将具有预测的降水量。
奇怪的是,当程序读取文件时,Data_Y 有一个形状 ( hhhh, 5),它应该是 (yyyy, 1)。
而且我认为这导致了这里的所有错误。
输入文件link如下
如何解决这个问题?请伸出援助之手。
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
tf.reset_default_graph()
tf.set_random_seed(777) # reproducibility
def MinMaxScaler(data):
numerator = data - np.min(data, 0)
denominator = np.max(data, 0) - np.min(data, 0)
# noise term prevents the zero division
return numerator / (denominator + 1e-7)
# train Parameters
seq_length = 6
data_dim = 5
hidden_dim = 10
output_dim = 1
learning_rate = 0.01
iterations = 500
# Open, High, Low, Volume, Close
#df = pd.read_csv("precipitation_post.csv", quotechar='"', decimal=".")
#df = df.interpolate(method ='linear', limit_direction ='forward')
#xy = df.reindex(index=df.index[::-1])
xy = np.loadtxt('df.txt', dtype='double', delimiter=' ', skiprows=1)
#xy = xy[::-1]
# train/test split
train_size = int(len(xy) * 0.7)
train_set = xy[0:train_size]
test_set = xy[train_size - seq_length:] # Index from [train_size - seq_length] to utilize past sequence
# Scale each
train_set = MinMaxScaler(train_set)
test_set = MinMaxScaler(test_set)
x = xy
y = xy[:, [-1]] # close as label
# build datasets
def build_dataset(time_series, seq_length):
dataX = []
dataY = []
for i in range(0, len(time_series) - seq_length):
_x = time_series[i:i + seq_length]
_y = time_series[i + seq_length]
print(_x, "->", _y)
dataX.append(_x)
dataY.append(_y)
return np.array(dataX), np.array(dataY)
trainX, trainY = build_dataset(train_set, seq_length)
testX, testY = build_dataset(test_set, seq_length)
# input place holders
X = tf.placeholder(tf.float32, shape=[None, seq_length, data_dim])
Y = tf.placeholder(tf.float32, shape=[None, 1])
# build a LSTM network
cell = tf.contrib.rnn.BasicLSTMCell(
num_units=hidden_dim, state_is_tuple=True, activation=tf.tanh)
outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
Y_pred = tf.contrib.layers.fully_connected(
outputs[:, -1], output_dim, activation_fn=None) # We use the last cell's output
# cost/loss
loss = tf.reduce_sum(tf.square(Y_pred - Y)) # sum of the squares
# optimizer
optimizer = tf.train.AdamOptimizer(learning_rate)
train = optimizer.minimize(loss)
# RMSE
targets = tf.placeholder(tf.float32, [None, 1])
predictions = tf.placeholder(tf.float32, [None, 1])
rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions)))
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
# Training step
for i in range(iterations):
_, step_loss = sess.run([train, loss], feed_dict={
X: trainX, Y: trainY})
print("[step: {}] loss: {}".format(i, step_loss))
# Test step
test_predict = sess.run(Y_pred, feed_dict={X: testX})
rmse_val = sess.run(rmse, feed_dict={
targets: testY, predictions: test_predict})
print("RMSE: {}".format(rmse_val))
# Plot predictions
plt.plot(testY)
plt.plot(test_predict)
plt.xlabel("Time Period")
plt.ylabel("Precipitation")
plt.show()
根据您提供的信息,这里有一个解决方案。很明显,您可能已经意识到,问题出在 build_dataset
函数中。您需要将函数更改为以下内容。
def build_dataset(data, seq_length):
dataX = []
dataY = []
for i in range(seq_length):
dataX.append(data[i:data.shape[0]-(seq_length-i)].reshape(-1, 1, 5))
dataX = np.concatenate(dataX, axis=1)
dataY = data[i+1:train_set.shape[0],4].reshape(-1, 1)
return dataX, dataY
此函数returns数据按以下方式处理。假设您有以下几行,
22.90 20.20 31.00 93.00 0.00
22.90 21.20 26.00 91.00 0.00
22.40 20.20 27.40 89.00 0.00
22.40 15.40 29.00 90.00 0.00
21.30 14.40 26.00 82.00 0.00
21.50 20.20 23.00 96.00 0.00
22.10 17.20 23.60 97.00 20.70
它给出 X
作为,
22.90 20.20 31.00 93.00 0.00
22.90 21.20 26.00 91.00 0.00
22.40 20.20 27.40 89.00 0.00
22.40 15.40 29.00 90.00 0.00
21.30 14.40 26.00 82.00 0.00
21.50 20.20 23.00 96.00 0.00
并且输出 Y
为 20.70
对于完整的数据集,它会产生以下形状。
Input: (6165, 6, 5)
Output: (6165, 1)
> WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
* https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
* https://github.com/tensorflow/addons
If you depend on functionality not listed there, please file an issue.
WARNING:tensorflow:From C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise/TimeSeriespy_RNN.py:74: BasicLSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This class is equivalent as tf.keras.layers.LSTMCell, and will be replaced by that in Tensorflow 2.0.
WARNING:tensorflow:From C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise/TimeSeriespy_RNN.py:75: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.
Instructions for updating:
Please use `keras.layers.RNN(cell)`, which is equivalent to this API
WARNING:tensorflow:From C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\tensorflow\python\ops\tensor_array_ops.py:162: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
Traceback (most recent call last):
File "<ipython-input-1-7716630f4e29>", line 1, in <module>
runfile('C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise/TimeSeriespy_RNN.py', wdir='C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise')
File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise/TimeSeriespy_RNN.py", line 97, in <module>
X: trainX, Y: trainY})
File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
run_metadata_ptr)
File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1128, in _run
str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (6165, 5) for Tensor 'Placeholder_1:0', which has shape '(?, 1)'
我遇到了一个错误,我只是检查了每个变量的维度,它看起来没有任何问题...你能告诉我哪里出了问题以及如何解决吗?
我想做的是天气预报。 输入形状将是 ( xxxx , 5),这里 xxxx 是输入数据的行数,5 是输入的类型,包括平均温度等。
输出形状必须是 (yyyy, 1),因为它的列将具有预测的降水量。
奇怪的是,当程序读取文件时,Data_Y 有一个形状 ( hhhh, 5),它应该是 (yyyy, 1)。
而且我认为这导致了这里的所有错误。
输入文件link如下
如何解决这个问题?请伸出援助之手。
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
tf.reset_default_graph()
tf.set_random_seed(777) # reproducibility
def MinMaxScaler(data):
numerator = data - np.min(data, 0)
denominator = np.max(data, 0) - np.min(data, 0)
# noise term prevents the zero division
return numerator / (denominator + 1e-7)
# train Parameters
seq_length = 6
data_dim = 5
hidden_dim = 10
output_dim = 1
learning_rate = 0.01
iterations = 500
# Open, High, Low, Volume, Close
#df = pd.read_csv("precipitation_post.csv", quotechar='"', decimal=".")
#df = df.interpolate(method ='linear', limit_direction ='forward')
#xy = df.reindex(index=df.index[::-1])
xy = np.loadtxt('df.txt', dtype='double', delimiter=' ', skiprows=1)
#xy = xy[::-1]
# train/test split
train_size = int(len(xy) * 0.7)
train_set = xy[0:train_size]
test_set = xy[train_size - seq_length:] # Index from [train_size - seq_length] to utilize past sequence
# Scale each
train_set = MinMaxScaler(train_set)
test_set = MinMaxScaler(test_set)
x = xy
y = xy[:, [-1]] # close as label
# build datasets
def build_dataset(time_series, seq_length):
dataX = []
dataY = []
for i in range(0, len(time_series) - seq_length):
_x = time_series[i:i + seq_length]
_y = time_series[i + seq_length]
print(_x, "->", _y)
dataX.append(_x)
dataY.append(_y)
return np.array(dataX), np.array(dataY)
trainX, trainY = build_dataset(train_set, seq_length)
testX, testY = build_dataset(test_set, seq_length)
# input place holders
X = tf.placeholder(tf.float32, shape=[None, seq_length, data_dim])
Y = tf.placeholder(tf.float32, shape=[None, 1])
# build a LSTM network
cell = tf.contrib.rnn.BasicLSTMCell(
num_units=hidden_dim, state_is_tuple=True, activation=tf.tanh)
outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
Y_pred = tf.contrib.layers.fully_connected(
outputs[:, -1], output_dim, activation_fn=None) # We use the last cell's output
# cost/loss
loss = tf.reduce_sum(tf.square(Y_pred - Y)) # sum of the squares
# optimizer
optimizer = tf.train.AdamOptimizer(learning_rate)
train = optimizer.minimize(loss)
# RMSE
targets = tf.placeholder(tf.float32, [None, 1])
predictions = tf.placeholder(tf.float32, [None, 1])
rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions)))
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
# Training step
for i in range(iterations):
_, step_loss = sess.run([train, loss], feed_dict={
X: trainX, Y: trainY})
print("[step: {}] loss: {}".format(i, step_loss))
# Test step
test_predict = sess.run(Y_pred, feed_dict={X: testX})
rmse_val = sess.run(rmse, feed_dict={
targets: testY, predictions: test_predict})
print("RMSE: {}".format(rmse_val))
# Plot predictions
plt.plot(testY)
plt.plot(test_predict)
plt.xlabel("Time Period")
plt.ylabel("Precipitation")
plt.show()
根据您提供的信息,这里有一个解决方案。很明显,您可能已经意识到,问题出在 build_dataset
函数中。您需要将函数更改为以下内容。
def build_dataset(data, seq_length):
dataX = []
dataY = []
for i in range(seq_length):
dataX.append(data[i:data.shape[0]-(seq_length-i)].reshape(-1, 1, 5))
dataX = np.concatenate(dataX, axis=1)
dataY = data[i+1:train_set.shape[0],4].reshape(-1, 1)
return dataX, dataY
此函数returns数据按以下方式处理。假设您有以下几行,
22.90 20.20 31.00 93.00 0.00
22.90 21.20 26.00 91.00 0.00
22.40 20.20 27.40 89.00 0.00
22.40 15.40 29.00 90.00 0.00
21.30 14.40 26.00 82.00 0.00
21.50 20.20 23.00 96.00 0.00
22.10 17.20 23.60 97.00 20.70
它给出 X
作为,
22.90 20.20 31.00 93.00 0.00
22.90 21.20 26.00 91.00 0.00
22.40 20.20 27.40 89.00 0.00
22.40 15.40 29.00 90.00 0.00
21.30 14.40 26.00 82.00 0.00
21.50 20.20 23.00 96.00 0.00
并且输出 Y
为 20.70
对于完整的数据集,它会产生以下形状。
Input: (6165, 6, 5)
Output: (6165, 1)