带嵌入的RNN

问题描述 投票:0回答:1

基于this方法,我试图建立具有分类和连续变量的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")

编辑:所有tensorflow图形代码运行正常。但是当我执行会话代码时,我收到以下错误:

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")

编辑结束

有人可以告诉我我在哪里弄错了吗?

谢谢!

tensorflow embedding
1个回答
1
投票

正如我所说,如果你想给每个时间步骤一个预测值,你应该将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")
© www.soinside.com 2019 - 2024. All rights reserved.