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

时间:2016-08-05 03:55:23

标签: tensorflow recurrent-neural-network

我最近开始学习Tensorflow并尝试使用扫描功能制作简单的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:形状(2,16)和(2,2,16)不兼容

请有人知道原因,告诉我如何解决它......

1 个答案:

答案 0 :(得分:0)

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