tensorflow - tensorflow 中的简单 LSTM 实现:考虑将元素转换为支持的类型错误
问题描述
我正在尝试在 Tensorflow 上实现一个简单的 LSTM 单元,以将其性能与我之前实现的另一个单元进行比较。
x = tf.placeholder(tf.float32,[BATCH_SIZE,SEQ_LENGTH,FEATURE_SIZE])
y = tf.placeholder(tf.float32,[BATCH_SIZE,SEQ_LENGTH,FEATURE_SIZE])
weights = { 'out': tf.Variable(tf.random_normal([FEATURE_SIZE, 8 * FEATURE_SIZE, NUM_LAYERS]))}
biases = { 'out': tf.Variable(tf.random_normal([4 * FEATURE_SIZE, NUM_LAYERS]))}
def RNN(x, weights, biases):
x = tf.unstack(x, SEQ_LENGTH, 1)
lstm_cell = tf.keras.layers.LSTMCell(NUM_LAYERS)
outputs = tf.keras.layers.RNN(lstm_cell, x, dtype=tf.float32)
return outputs
pred = RNN(x, weights, biases)
# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
我使用了我在 GitHub 上找到的示例并尝试对其进行更改以获得我想要的行为,但我收到了以下错误消息:
TypeError: Failed to convert object of type <class 'tensorflow.python.keras.layers.recurrent.RNN'> to Tensor. Contents: <tensorflow.python.keras.layers.recurrent.RNN object at 0x7fe437248710>. Consider casting elements to a supported type.
解决方案
尝试
outputs = tf.keras.layers.RNN(lstm_cell, dtype=tf.float32) (x)
反而
以下是TF 文档中的示例:
# Let's use this cell in a RNN layer:
cell = MinimalRNNCell(32)
x = keras.Input((None, 5))
layer = RNN(cell)
y = layer(x)
# Here's how to use the cell to build a stacked RNN:
cells = [MinimalRNNCell(32), MinimalRNNCell(64)]
x = keras.Input((None, 5))
layer = RNN(cells)
y = layer(x)
推荐阅读
- ios - Apple Pay 不适用于发布版本(适用于调试)
- c - 如何以小端顺序C发送数据
- docker - 在 Linux 上挂载大量文件的 Docker 卷缓慢
- c++ - 单行 std::get std::index_sequence?
- javascript - 单击手风琴项时如何保留一个图标名称?
- java - 在 Spring 集成中将动态值设置为生存时间
- java - 在java中将RAW图像转换为jpg
- python - 你如何把一个里面有一个整数的元组变成一个字符串或一个整数
- python - 如何生成预算受限的加权随机样本,其中项目具有不同的概率和权重?
- python-3.x - 使用 Python 和 Arduino 通过串行更改灯泡的延迟