【发布时间】:2020-03-27 05:38:21
【问题描述】:
> 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)。
我认为这导致了这里的所有错误。
输入文件的链接如下
我该如何解决这个问题?请给我你的帮助。
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()
【问题讨论】:
-
trainX的形状是什么? -
train_X的形状是(6165,6,5)
-
你的 train_Y 有形状 (6165,1) 吗?
-
对所有的困惑感到抱歉。我把截图放在那里,demention 是 (6165,5)。
-
这就是问题所在。你的
Y = tf.placeholder(tf.float32, shape=[None, 1])。但是您正试图通过 (6165, 5)。请注意,最后一个维度需要匹配。因此你得到一个错误。
标签: tensorflow time-series artificial-intelligence prediction recurrent-neural-network