请选择 进入手机版 | 继续访问电脑版
查看: 2450|回复: 2

错误总结

[复制链接]

665

主题

1234

帖子

6561

积分

xdtech

Rank: 5Rank: 5

积分
6561
发表于 2019-1-3 11:57:30 | 显示全部楼层 |阅读模式
本帖最后由 shaoheshaohe 于 2019-1-3 11:59 编辑

1. 没有 unpack

出现如下错误

AttributeError: module 'tensorflow' has no attribute 'unpack'

    1

原因是 tf.unpack 改为了 tf.unstack

# 原代码
x_ = tf.unpack(x, axis=1)

# 修改为
x_ = tf.unstack(x, axis=1)

    1
    2
    3
    4
    5

2. 没有 rnn_cell

出现如下错误

AttributeError: module 'tensorflow.python.ops.nn' has no attribute 'rnn_cell'

    1

原因是 tf.nn.rnn_cell 改为了 tf.contrib.rnn

# 原代码
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(HIDDEN_SIZE)
    cell = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * NUM_LAYERS)

# 修改为
lstm_cell = tf.contrib.rnn.BasicLSTMCell(HIDDEN_SIZE)
    cell = tf.contrib.rnn.MultiRNNCell([lstm_cell] * NUM_LAYERS)

    1
    2
    3
    4
    5
    6
    7

3. rnn 不可调用

出现如下错误

TypeError: 'module' object is not callable

    1

原因是 tf.nn.rnn 现在改为了几个方法

tf.contrib.rnn.static_rnn
tf.contrib.rnn.static_state_saving_rnn
tf.contrib.rnn.static_bidirectional_rnn
tf.contrib.rnn.stack_bidirectional_dynamic_rnn

    1
    2
    3
    4

而我们需要的是 tf.nn.dynamic_rnn() 方法

# 原代码
output, _ = tf.nn.rnn(cell, X, dtype=tf.float32)

# 修改为
output, _ = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)

    1
    2
    3
    4
    5

4. 不能调用 Estimator.fit

出现如下警告

WARNING:tensorflow:From train.py:71: calling BaseEstimator.fit (from tensorflow.contrib.learn.python.learn.estimators.estimator) with y is deprecated and will be removed after 2016-12-01.

    1

该警告下面给出了解决方法

Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
  est = Estimator(...) -> est = SKCompat(Estimator(...))

    1
    2
    3
    4
    5
    6

按照给出的方法修改代码

# 原代码
regressor = learn.Estimator(model_fn=lstm_model)

# 修改为
from tensorflow.contrib.learn.python.learn.estimators.estimator import SKCompat

regressor = SKCompat(learn.Estimator(model_fn=lstm_model))

    1
    2
    3
    4
    5
    6
    7

5. 临时文件夹

出现如下警告

WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmp01x9hws6

    1

原因是现在的 Estimator 需要提供 model_dir

# 原代码
regressor = SKCompat(learn.Estimator(model_fn=lstm_model))

# 修改为
regressor = SKCompat(
    learn.Estimator(model_fn=lstm_model, model_dir='model/'))

    1
    2
    3
    4
    5
    6

6. 尺寸必须一致

出现如下错误

ValueError: Dimensions must be equal, but are 60 and 40
for 'rnn/rnn/multi_rnn_cell/cell_0/basic_lstm_cell/MatMul_1'
(op: 'MatMul') with input shapes: [?,60], [40,120].

    1
    2
    3

原因我不太清楚,可能是因为 TensorFlow 的调整导致生成的数据在形状上与老版本不一致,也可能是因为使用 lstm_cell*NUM_LAYERS 的方法创建深层循环网络模型导致每层 LSTM 的 tensor 名称都一样

只能在网上搜了其他的类似的博客后照着修改了代码,下面给出了修改的关键地方,详细的部分在完整代码中

# LSTM结构单元
def LstmCell():
    lstm_cell = tf.contrib.rnn.BasicLSTMCell(HIDDEN_SIZE)
    return lstm_cell

def lstm_model(X, y):
    # 使用多层LSTM,不能用lstm_cell*NUM_LAYERS的方法,会导致LSTM的tensor名字都一样
    cell = tf.contrib.rnn.MultiRNNCell([LstmCell() for _ in range(NUM_LAYERS)])
    # 将多层LSTM结构连接成RNN网络并计算前向传播结果
    output, _ = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
    ......

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

7. Legend 不支持

出现如下错误

UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x7feb52d58c18>] instances.
A proxy artist may be used instead.

    1
    2

原因是因为需要在调用 plt.plot 时参数解包

# 原代码
plot_predicted = plt.plot(predicted, label='predicted')
plot_test = plt.plot(test_y, label='real_sin')

# 修改为(加逗号)
plot_predicted, = plt.plot(predicted, label='predicted')
plot_test, = plt.plot(test_y, label='real_sin')

    1
    2
    3
    4
    5
    6
    7

8. 使用 plt.show() 不显示图片

在代码中使用 plt.show() 运行之后没有图片显示,原因是原代码中使用了 mpl.use(‘Agg’),而 Agg 是不会画图的,所以直接把这一行删掉
9. get_global_step 不建议使用

出现如下警告

WARNING:tensorflow:From train.py:60: get_global_step
(from tensorflow.contrib.framework.python.ops.variables)
is deprecated and will be removed in a future version.

    1
    2
    3

警告下面给出了解决方法

Instructions for updating:
Please switch to tf.train.get_global_step

    1
    2

按照解决方法修改代码

# 原代码
train_op = tf.contrib.layers.optimize_loss(
        loss,
        tf.contrib.framework.get_global_step(),
        optimizer='Adagrad',
        learning_rate=0.1)

# 修改为
train_op = tf.contrib.layers.optimize_loss(
        loss,
        tf.train.get_global_step(),
        optimizer='Adagrad',
        learning_rate=0.1)
---------------------  
作者:widiot8023  
来源:CSDN  
原文:https://blog.csdn.net/white_idiot/article/details/78882856  
版权声明:本文为博主原创文章,转载请附上博文链接!
回复

使用道具 举报

166

主题

616

帖子

1万

积分

xdtech

Rank: 5Rank: 5

积分
10667
发表于 2019-1-4 16:02:49 | 显示全部楼层
rnn实在是大坑
tensorflow不停地变
前面的实现
后面竟然不兼容
真是醉了
回复

使用道具 举报

166

主题

616

帖子

1万

积分

xdtech

Rank: 5Rank: 5

积分
10667
发表于 2019-1-31 17:20:40 | 显示全部楼层
这些错误
都是很常见的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表