tensorflow-004-RNN01

LSTM RNN


tf.nn.rnn_cell.BasicLSTMCell.

__call__(inputs, state) # inputs [batch_size, input_size]  #[:, t, :]

__init__(num_units, ...)

output_size

state_size

zero_state(batch_size, dtype)


import tensorflow as tf 
import numpy as np 
import sys
from tensorflow.examples.tutorials.mnist import input_data
import os



datai = tf.placeholder(tf.float32, shape=[3,10, 28])
label = tf.placeholder(tf.float32, [None, 10])

data = tf.transpose(datai, [1, 0, 2])
data = tf.reshape(data, [-1, 28])
data = tf.split(0, 10, data)

lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(128, state_is_tuple=True)
print (lstm_cell.state_size)

init_state = [tf.zeros([3, 128])]*2
#init_state = [(init_state, init_state)]*3
#init_state = tf.zeros([3, 128])
#init_state = tf.split(0, 3, init_state)

outx = []

#ini = lstm_cell.zero_state(3, dtype=tf.float32) # ([3, 128], [3, 128])
#print ini[0].get_shape().as_list()

with tf.variable_scope("LSTM") as scope:
	for t in xrange(10):
		if t> 0:
			scope.reuse_variables()
		out, init_state = lstm_cell(data[t],  init_state)

		#datax = tf.split(0, 3, data[t])
		#for per in xrange(3):
		#	if per>0 or t>0:
		#		scope.reuse_variables()
		#	#print datai[per, t, :].get_shape()
		#	out, init_state[per] = lstm_cell(datax[per],  init_state[per])  ##
		#	print init_state[per][0].get_shape()
		outx.append(out)

#outs, state = tf.nn.rnn(lstm_cell, input[list-[batch_size, input_size]] ,initialize)
#outs, state = tf.nn.dynamic_rnn(lstm_cell, input[tensor-[batch_size, step_n, input_size]], initial_state)

inputdata = np.random.randn(3,10,28)

with tf.Session() as sess:
	sess.run( tf.initialize_all_variables())

	o = sess.run(outx, feed_dict = { datai: inputdata})
	print np.array(o).shape







----------------------------------------------reference----------------------------------------------

http://blog.csdn.net/mydear_11000/article/details/52414342

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值