带嵌入的 RNN

RNN with Embedding

基于 方法,我尝试构建具有分类变量和连续变量的 RNN 模型。

连续占位符是这种形式:

x = tf.placeholder(tf.float32, [None,num_steps, input_size], name="input_x")`

And the categorical data placeholder is in this form:

store, v_store = len(np.unique(data_df.Store.values)), 50

z_store = tf.placeholder(tf.int32, [None, num_steps], name='Store')

emb_store = tf.Variable(
    tf.random_uniform((store, v_store), -r_range, r_range),
    name="store"
    )

embed_store = tf.nn.embedding_lookup(emb_store, z_store)

最后,我将分类占位符和连续占位符连接在一起。

inputs_with_embed = tf.concat([x, embed_store], axis=2, name="inputs_with_embed")

这是我将张量向量与最后一层相乘的地方。

val = tf.transpose(val, [1, 0, 2])
last = tf.gather(val, int(val.get_shape()[0]) - 1, name="lstm_state")
ws = tf.Variable(tf.truncated_normal([lstm_size, input_size]), name="w")
bias = tf.Variable(tf.constant(0.1, shape=[input_size]), name="b")

编辑: 所有张量流图代码 运行 都很好。但是当我执行会话代码时,出现以下错误:

InvalidArgumentError (see above for traceback): Incompatible shapes: [50,4] vs. [50,7,1]
   [[Node: sub = Sub[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](add, _arg_input_y_0_4)]]

还有我的预测部分。

loss = tf.reduce_mean(tf.square(pred - y), name="loss_mse_train")

编辑结束

谁能告诉我哪里出错了?

谢谢!

正如我所说,如果你想给每个时间步一个预测值,你应该将ws更改为[lstm_size, 7],将bias更改为[7]

ws = tf.Variable(tf.truncated_normal([lstm_size, 7]), name="w")
bias = tf.Variable(tf.constant(0.1, shape=[7]), name="b")

# need to change shape when pred=(?,7) and y=(?,7,1) 
loss = tf.reduce_mean(tf.square(pred - tf.squeeze(y)), name="loss_mse_train")