【问题标题】:How to control output-size from LSTM cell in tensorflow如何在张量流中控制 LSTM 单元的输出大小
【发布时间】:2023-03-21 19:35:01
【问题描述】:

我试图构建一个简单的例子,通过 Tensorflow 使用 LSTM RNN 来预测一些目标序列的时间序列值,给定已知的输入时间序列。

Link to example problem

我正在尝试

what I try to accomplish formally

本质上,我认为单元格 A 的输出和以下矩阵 mult 应该起到以下作用:

X = np.zeros([40,2,1])
A = np.zeros([40,1,2])
b = np.arange(0,2)

X = tf.convert_to_tensor(X)
A = tf.convert_to_tensor(A)
b = tf.convert_to_tensor(b)

Y = tf.matmul(X,A)+b

tensorflow 代码设置为查看输出大小,而不是功能性 tf.graph/session:

import numpy as np
import tkinter
import matplotlib.pyplot as plt
import tensorflow as tf
n=40
x = np.linspace(0,10,n)
y1 = np.sin(x)
y2 = np.cos(x)

x1=np.random.normal(0,y1**2,n)
x2=np.random.normal(0,y2**2,n)

y1=(y1**2>0.4)*1
y2=(y2**2>0.4)*1

ys = np.vstack((y1,y2))
xs = np.vstack((x1,x2))

def plot_results_multiple(xs, ys):
    fig = plt.figure(facecolor='white')
    ax = fig.add_subplot(111)
    for i, data in enumerate(xs):
        plt.plot(data, label='x'+str(i))
        plt.legend()
    for i, data in enumerate(ys):
        plt.plot(data, label='y'+str(i))
        plt.legend()
    plt.show()

plot_results_multiple(xs,ys)

xs = xs.T
ys = ys.T

print("Shape of arrays " +str(xs.shape) + " " +str(ys.shape))


batch_size = 1
lstm_size = 1
nseries = 2
time_steps = 40
nclasses = 2

lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size,state_is_tuple=True)
stacked_lstm = tf.contrib.rnn.MultiRNNCell([lstm] * 2, state_is_tuple=True)

state = lstm.zero_state(batch_size, tf.float32)
inputs = tf.unstack(xs, num=40, axis=0)

outputs = []

with tf.variable_scope("RNN"):
    for timestep in range(time_steps):
        if timestep > 0: tf.get_variable_scope().reuse_variables()
        output, state = lstm(tf.cast(tf.reshape(inputs[timestep],[1,nseries]),tf.float32), state)
        print(tf.convert_to_tensor(output).get_shape())
        outputs.append(output)

print(tf.convert_to_tensor(outputs).get_shape())
output = tf.reshape(tf.concat(outputs, 1), [-1, lstm_size])
softmax_w = tf.get_variable(
    "softmax_w", [time_steps, 1,nclasses],tf.float32)# dtype=
print(softmax_w.get_shape())
softmax_b = tf.get_variable("softmax_b", [nseries], dtype=tf.float32)
print(softmax_b.get_shape())
logits = tf.matmul(output, softmax_w) + softmax_b

print(logits.get_shape())

我认为我遇到的问题是弄清楚如何修改 RNN LSTM 单元,因为它当前正在从 2x1 输入输出 1x1 张量,而我期望输出 2x1。非常感谢任何帮助。

【问题讨论】:

  • 貌似是由tf.contrib.rnn.BasicLSTMCell(lstm_size,state_is_tuple=True)的第一个参数隐藏的神经元数控制的

标签: python tensorflow lstm recurrent-neural-network


【解决方案1】:

由tf.contrib.rnn.BasicLSTMCell(lstm_size,state_is_tuple)的第一个参数隐藏的神经元单元控制

源代码在这里: https://github.com/tensorflow/tensorflow/blob/b0ecc7d2c1486367ec65d297e372f8935ee3ddfe/tensorflow/python/ops/rnn_cell_impl.py#254

@property
  def output_size(self):
    return self._num_units

所以如果你想改变输出大小,你需要改变tf.contrib.rnn.BasicLSTMCell的num_units。

【讨论】:

  • 我尝试覆盖output_size 属性,它会抛出ValueError 情况,输出的形状与输入不同。所以 output_size 必须等于 _num_units ?有什么建议吗?
猜你喜欢
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 2019-04-08
相关资源
最近更新 更多