python - TensorFlow 中的 RNN 单元命名问题

标签 python machine-learning tensorflow

我正在学习 packtpub 视频系列中的 TensorFlow 教程。不幸的是,教程中的基本 RNN 似乎不再起作用,或者发生了一些奇怪的事情。有什么见解吗?

这是我收到的错误:

ValueError:变量 RNN/BasicRNNCell/Linear/Matrix 已存在,不允许。您的意思是在 VarScope 中设置reuse=True 吗?最初定义于:

File "<ipython-input-23-dcf4ba3c6842>", line 16, in <module>
    outputs, states = tf.nn.dynamic_rnn(cell, x_, dtype = tf.float32, initial_state = None)
  File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2869, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2809, in run_ast_nodes
    if self.run_code(code, result):

错误似乎表明矩阵或其他东西

这是它引用的代码

import requests
import numpy as np
import math
import tensorflow as tf
import datetime
from tqdm import tqdm

dataUrl = "https://drcdata.blob.core.windows.net/data/weather.npz"
response = requests.get(dataUrl)
with open("weather.zip", "wb") as code:
    code.write(response.content)
#load into np array
data = np.load("weather.zip")  
daily = data['daily']
weekly = data['weekly']

更多代码

num_weeks = len(weekly)
dates = np.array([datetime.datetime.strptime(str(int(d)), '%Y%m%d') for d in weekly[:,0]])
def assign_season(date):
    month = date.month
    #spring = 0
    if 3 <= month < 6:
        season = 0
    #summer = 1
    elif 6 <= month < 9:
        season = 1
    elif 9 <= month < 12:
        season = 2
    elif month == 12 or month < 3:
        season = 3
    return season

更多代码

num_classes = 4
num_inputs = 5
#Historical state for RNN size
state_size = 11

labels = np.zeros([num_weeks, num_classes])
#read and convert to one-hot
for i,d in enumerate(dates):
    labels[i,assign_season(d)] = 1

#extract and scale training data
train = weekly[:,1:]
train = train - np.average(train,axis=0)
train = train / train.std(axis = 0)

sess = tf.InteractiveSession()

#Inputs
x = tf.placeholder(tf.float32, [None, num_inputs])

#Special RNN TF Input Shape
x_ = tf.reshape(x, [1, num_weeks, num_inputs])

#Define the labels
y_ = tf.placeholder(tf.float32, [None, num_classes])

#Define RNN Cell
#RNN's method for looking back in time.
cell = tf.nn.rnn_cell.BasicRNNCell(state_size)
#Intelligently handles recursion instead of unrolling full computation.
outputs, states = tf.nn.dynamic_rnn(cell, x_, dtype = tf.float32, initial_state = None)

#Define Weights and Biases
W1 = tf.Variable(tf.truncated_normal([state_size, num_classes], stddev = 1.0 / math.sqrt(num_inputs)))
b1 = tf.Variable(tf.constant(0.1, shape = [num_classes]))

#reshape output for normal usage
#h1 = tf.reshape(outputs, [-1, state_size])

#softmax output, remember, its a classifier
y = tf.nn.softmax(tf.matmul(h1, W1) + b1)

训练 IT 代码

sess.run(tf.initialize_all_variables())

#Define Cost Function
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y + 1e-50, y_))

#define train step
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

#Define Accuracy
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

#Really train this thing.
epochs = 500
train_acc = np.zeros(epochs//10)
test_acc = np.zeros(epochs//10)

for i in tqdm(range(epochs), ascii=True):
    if i % 10 == 0: #record for learning curve display
        A = accuracy.eval(feed_dict={x: train, y_: labels})
        train_acc[i//10] = A
    train_step.run(feed_dict={x: train, y_:labels})

绘制一些东西

 %matplotlib inline
import matplotlib.pyplot as plt
plt.plot(train_acc)

最佳答案

尝试清除默认图表或重置图表(请参阅 Remove nodes from graph or reset entire default graph )。在声明我的图表后,我遇到了同样的错误

with tf.Session() as sess:

并重置默认图表解决了我的问题。我的猜测是,iPython Notebook 在调用笔记本单元之间保持图形状态相同,而当问题作为脚本运行时,图形在每次运行后都会被清除。

关于python - TensorFlow 中的 RNN 单元命名问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38676668/

相关文章:

python - 爱彼迎 Airflow : Installing Airflow without pip

python - 为什么手动计算的MSE与sklearn中的LassoCV.mse_path不同

python - 通过对单元格进行分组在 pandas 列上绘制直方图

python - 如何使按钮立即禁用?

python - 凯拉斯 + tensorflow : 'ConvLSTM2D' object has no attribute 'outbound_nodes'

python - 通过加载训练有素的模型进行多重处理。 (通过Python3)

python - 如何将 sklearn MinMaxScaler() 的值转换回实际值?

python - 在dynamic_rnn和LSTMCell中使用带有输入字符串的TensorFlow时出错

树莓派 3 : i wasn't able to write a value in db using a variable 上的 python 和 sql

machine-learning - 如何在svm(scikit)中提取和传输学习参数?