最近搭建LSTM网络,各个参数都初始化好了,但是进行参数更新时就出错
下面是出错提示
FailedPreconditionError: Error while reading resource variable rnn/multi_rnn_cell/cell_2/layer_2/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/rnn/multi_rnn_cell/cell_2/layer_2/bias/class tensorflow::Var does not exist.
[[node rnn/multi_rnn_cell/cell_2/layer_2/bias/Read/ReadVariableOp (defined at G:/project/LSTM/LSTM.py:65) ]]
下面是具体的代码
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
from PIL import Image
from loadData import loadData
from tensorflow.contrib import rnn
import os
tf.Graph().as_default()
img_path = r"G:/project/dataset/state-farm-distracted-driver-detection/train"
# 设置用到的参数
lr = 1e-3
# 在训练和测试的时候 想使用不同的batch_size 所以采用占位符的方式
'''
batch_size = tf.placeholder(tf.int32, [])
'''
keep_prob = tf.placeholder(dtype =tf.float32)
# 输入数据是128维 一行 有128个像素
input_size = 128
# 时序持续时长为128 每做一次预测,需要先输入28行
timestep_size = 128
# 每个隐含层的节点数
hidden_size = 256
# LSTM的层数
layer_num = 3
# 最后输出的分类类别数量,如果是回归预测的呼声应该是1
class_num = 10
_batch_size = 10
keep_prob_ = 1.0
_keep_prob = 0.5
X = tf.placeholder(dtype =tf.float32, shape=[None, 16384],name = 'input')
y = tf.placeholder(dtype =tf.float32, shape=[None, class_num])
_X = tf.reshape(X,[-1,timestep_size,input_size])
def unit_lstm(j):
# 定义一层LSTM_CELL hiddensize 会自动匹配输入的X的维度
lstm_cell = rnn.BasicLSTMCell(num_units=hidden_size, forget_bias=1.0, state_is_tuple=True,name = 'layer_%s'%j)
# 添加dropout layer, 一般只设置output_keep_prob
Dropout_Wrapper = rnn.DropoutWrapper(cell=lstm_cell, input_keep_prob=1.0, output_keep_prob=keep_prob)
return Dropout_Wrapper
# 调用MultiRNNCell来实现多层 LSTM
mlstm_cell = rnn.MultiRNNCell(cells =[unit_lstm(j) for j in range(layer_num)], state_is_tuple=True)
# 使用全零来初始化state
init_state = mlstm_cell.zero_state(batch_size=_batch_size, dtype=tf.float32)
outputs, state = tf.nn.dynamic_rnn(mlstm_cell, inputs=_X, initial_state = init_state, time_major = False)
#网络的最终输出
final_out = outputs[:, -1, :]
W = tf.Variable(initial_value = tf.truncated_normal([hidden_size, class_num], stddev=0.1), dtype=tf.float32)
bias = tf.Variable(initial_value = tf.constant(0.1, shape = [class_num]), dtype = tf.float32)
y_pre = tf.nn.softmax(tf.matmul(h_state, W) + bias)
# 损失和评估函数
cross_entropy = -tf.reduce_mean(y * tf.log(y_pre)) #交叉熵函数
train_op = tf.train.AdamOptimizer(lr).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 开始训练
sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
#训练,进行数据提取,batch=10,图片格式是128*128*3
for i in range(2):
data_feed = loadData(img_path,batch_size=_batch_size, train_shuffle=True)
image_batch, label_batch = data_feed.get_train()
sess = tf.Session()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess, coord)
image_batch_v, label_batch_v = sess.run([image_batch, label_batch])
img = (image_batch_v[:, :, :,0]+image_batch_v[:, :, :,1]+image_batch_v[:, :, :,2])/3.0
image = img.reshape(-1,16384)
print(label_batch_v)
optimizer_ = sess.run(train_op,feed_dict={X: image, y: label_batch_v, keep_prob:1.0})
if (i+1)%10 == 0:
train_accuracy = sess.run(accuracy, feed_dict = {X: image, y: label_batch_v,keep_prob:keep_prob_})
print("step %d, training accuracy %g" % ((i+1), train_accuracy ))
一旦运行到optimizer的时候就开始报错,请问是哪里出了问题?
是因为我利用占位符进行数据传递的问题还是说,是初始化的问题?