在 Tensorflow 中使用扫描功能制作简单的 rnn 代码

Making simple rnn code with scan function in Tensorflow

最近开始学习Tensorflow,尝试用scan函数做简单的rnn代码。 我要做的是让 RNN 预测正弦函数。 它得到 1 暗淡的输入。并按如下批量输出 1 个暗淡。

import tensorflow as tf
from tensorflow.examples.tutorials import mnist
import numpy as np
import matplotlib.pyplot as plt
import os
import time

# FLAGS (options)
tf.flags.DEFINE_string("data_dir", "", "")
#tf.flags.DEFINE_boolean("read_attn", True, "enable attention for reader")
#tf.flags.DEFINE_boolean("write_attn",True, "enable attention for writer")
opt = tf.flags.FLAGS

#Parameters
time_step = 10
num_rnn_h = 16
batch_size = 2
max_epoch=10000
learning_rate=1e-3 # learning rate for optimizer
eps=1e-8 # epsilon for numerical stability

#temporary sinusoid data
x_tr = np.zeros([batch_size,time_step])
y_tr = np.zeros([batch_size,time_step])
ptrn = 0.7*np.sin(np.arange(time_step+1)/(2*np.pi))
x_tr[0] = ptrn[0:time_step]
y_tr[0] = ptrn[1:time_step+1]
x_tr[1] = ptrn[0:time_step]
y_tr[1] = ptrn[1:time_step+1]

#Build model
x = tf.placeholder(tf.float32,shape=[batch_size,time_step,1], name= 'input')
y = tf.placeholder(tf.float32,shape=[None,time_step,1], name= 'target')
cell = tf.nn.rnn_cell.BasicRNNCell(num_rnn_h)
#cell = tf.nn.rnn_cell.LSTMCell(num_h, state_is_tuple=True)
with tf.variable_scope('output'):
    W_o = tf.get_variable('W_o', shape=[num_rnn_h, 1])
    b_o = tf.get_variable('b_o', shape=[1], initializer=tf.constant_initializer(0.0))

init_state = cell.zero_state(batch_size, tf.float32)

#make graph
#rnn_outputs, final_states = tf.scan(cell, xx1, initializer= tf.zeros([num_rnn_h]))
scan_outputs = tf.scan(lambda a, xi: cell(xi, a), tf.transpose(x, perm=[1,0,2]), initializer= init_state)
rnn_outputs, rnn_states = tf.unpack(tf.transpose(scan_outputs,perm=[1,2,0,3]))
print rnn_outputs, rnn_states

with tf.variable_scope('predictions'):
    weighted_sum = tf.reshape(tf.matmul(tf.reshape(rnn_outputs, [-1, num_rnn_h]), W_o), [batch_size, time_step, 1])
    predictions = tf.add(weighted_sum, b_o, name='predictions')
with tf.variable_scope('loss'):
    loss = tf.reduce_mean((y - predictions) ** 2, name='loss')

train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss)

但是它在最后一行(优化器)给出了一个错误,比如,

ValueError: Shapes (2, 16) and (2, 2, 16) are not compatible

请知道原因的人告诉我如何解决...

我假设您的错误不在最后一行(优化器),而是在您之前执行的某些操作。也许在 reduce_mean 中使用这个 y - 预测?我不会详细介绍您的代码,但我会告诉您,当您在需要相同形状的两个张量之间进行运算(通常是数学运算)时,会出现此错误。