我只是好奇如何生成一个序列,批次和/或时代来提供张量流模型,一个来自numpy数组的多层RNN图。最初的numpy数组是从pandas数据集和下面的Sklearn分割生成的。
从Numpy到Pandas
#define features and labels using X, Y from a numpy array
X = Input_Output_Matrix.iloc[:, 0:3].values
y = np.around(Input_Output_Matrix.iloc[:, 3], decimals=1).values
# Splitting the dataset into the Training set and Test set
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 0)
注意:非常重要
y_train.shape
缺货[37]: (6721,100)
X_train.shape
缺货[38]: (6721,3)
现在的形状
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X)
res = tf.one_hot(indices=y, depth=100)
with tf.Session() as sess:
y = sess.run(res)
为了生成配置参数。
# Configuration is wrapped in one object for easy tracking and passing.
class RNNConfig():
input_size = X_train.shape[1]
output_size = y_train.shape[1]
num_steps = 100
lstm_size = y_train.shape[0]//100
num_layers = 4
keep_prob = 0.8
batch_size = 100
init_learning_rate = 0.001
learning_rate_decay = 0.99
init_epoch = 5
max_epoch = 5000
DEFAULT_CONFIG = RNNConfig()
用于配置的输入参数实际上是基于numpy数组的形状,比如说3个输入的input_size = 3和来自一个热编码的输出的output_size = 100,即深度等于100。
#one hot encoding to generate 10 columns for the labels
res = tf.one_hot(indices=y, depth=100)
with tf.Session() as sess:
y = sess.run(res)
with multi_lstm_graph.as_default():
x_data = tf.placeholder(tf.float32, [None, DEFAULT_CONFIG.num_steps,
DEFAULT_CONFIG.input_size])
y_label = tf.placeholder(tf.float32, [None, DEFAULT_CONFIG.num_steps,
DEFAULT_CONFIG.output_size])
learning_rate = tf.placeholder(tf.float32, None)
def _create_one_cell():
lstm_cell = tf.contrib.rnn.LSTMCell(config.lstm_size,
state_is_tuple=True)
if config.keep_prob < 1.0:
lstm_cell = tf.contrib.rnn.DropoutWrapper(lstm_cell,
output_keep_prob=config.keep_prob)
return lstm_cell
cell = tf.contrib.rnn.MultiRNNCell([_create_one_cell() for _ in
range(config.num_layers)], state_is_tuple=True) if
config.num_layers > 1 else _create_one_cell()
val, _ = tf.nn.dynamic_rnn(cell, x_data, dtype=tf.float32)
val = tf.transpose(val, [1, 0, 2])
last = tf.gather(val, int(val.get_shape()[0]) - 1)
weight = tf.Variable(tf.truncated_normal([config.lstm_size,
config.input_size]))
bias = tf.Variable(tf.constant(0.01, shape=[config.input_size]))
y_pred = tf.matmul(last, weight) + bias
对于图表功能
张量流特征如下所示, #现在进行培训课程
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits
(logits=y_pred, labels=y_label), name="graph_loss")
optimizer = tf.train.AdamOptimizer(learning_rate)
minimize = optimizer.minimize(loss )
tf.summary.scalar("loss_mse", loss)
最后是培训课程
with tf.Session(graph=Multilayer_RNN_Graph_Cell) as sess:
tf.global_variables_initializer().run()
配置参数
learning_rates_to_use = [config.init_learning_rate*
(config.learning_rate_decay ** max(
float(i + 1 -config.init_epoch), 0.0)) for i in
range(config.max_epoch)]
test_data_feed = {inputs: X_test, targets: X_test, learning_rate: 0.0}
以下是我尝试迭代时代的方法。
for epoch_step in range(DEFAULT_CONFIG.max_epoch):
current_lr = learning_rates_to_use[epoch_step]
以下是基于输入数组的形状,特别是功能的数量,我的批次。
for _ in range(int(X_train.shape[0]/config.batch_size)):
rand_index = np.random.choice(len(X_train),
size=config.batch_size)
batch_X = X_train[rand_index].reshape((1, config.num_steps,
config.input_size))
#indexing of 1_D np.array
batch_y = y_train[rand_index].reshape((1, config.num_steps,
config.output_size))
'''Each loop below completes one epoch training.'''
train_data_feed = {inputs: batch_X,
targets: batch_y,
learning_rate: 0}
'''Each loop below completes one epoch training.'''
train_loss, _ = sess.run([loss, minimize], train_data_feed)
cost_history = np.append(cost_history, train_loss)
'''results of the Session'''
print('Epoch', epoch, 'completed out of', hm_epochs,'loss:',
cost_history)
'''In order to test for Model Accuracy '''
if epoch_step%10 == 0:
test_loss, _pred, _summary = sess.run([loss, prediction,
merged_summary], test_data_feed)
assert len(_pred) == len(y_test)
print ("Epoch %d [%f]:" % (epoch_step, current_lr), test_loss)
现在我出局了。我得到以下错误。我对logits_size = [1,3]有特殊的问题,我不知道它是如何生成的。它不与任何一个矩阵相关(输入矩阵,X_train或输出,矩阵,y_train。)。我的问题是如何将logits_size与labels_size = [100,100]匹配。
提前致谢
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
C:\Users\MAULIDI\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1326 try:
-> 1327 return fn(*args)
1328 except errors.OpError as e:
C:\Users\MAULIDI\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
1305 feed_dict, fetch_list, target_list,
-> 1306 status, run_metadata)
1307
C:\Users\MAULIDI\Anaconda3\lib\contextlib.py in __exit__(self, type, value, traceback)
87 try:
---> 88 next(self.gen)
89 except StopIteration:
C:\Users\MAULIDI\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py in raise_exception_on_not_ok_status()
465 compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 466 pywrap_tensorflow.TF_GetCode(status))
467 finally:
InvalidArgumentError: logits and labels must be same size: logits_size=[1,3] labels_size=[100,100]
[[Node: train/SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](train/Reshape, train/Reshape_1)]]
During handling of the above exception, another exception occurred:
答案 0 :(得分:2)
我认为这部分代码存在问题。
val = tf.transpose(val, [1, 0, 2])
last = tf.gather(val, int(val.get_shape()[0]) - 1)
RNN的输出是(timestep,batch_index,data),您将转置到(batch_index,timestep,data)。然后你会在轴0上收集indices = shape [0] - 1(这是默认值)。所以你要采用批次的最后一个元素。您可能想要指定轴1。
另一种方法是保持代码清洁:
last = val[:, -1, :]
我猜测你只是在测试中做了一次,所以应该解释1。 我现在没有看到任何其他错误所以我猜你的input_size是3,当你进行矩阵乘法时你会得到[1,3]。
检查重量是否有(x,100)的形状。如果您的批量大小为100,那么这两个应该给出具有正确形状的结果。