Tensorflow session.run TypeError

问题描述 投票:1回答:1

我正在尝试学习RL和tensorflow。不幸的是,代码中有一个问题,我无法解决。以下调用失败:

train_loss, _, train_summary = session.run([loss, opt, all_summary], feed_dict={x_ph: X, y_ph: y})

我收到以下错误:

TypeError:获取参数None具有无效的类型类'NoneType'

我正在使用Windows 10操作系统。

我在做什么错?

我真的需要一些帮助,谢谢。

这里是完整的代码:

import tensorflow as tf
import tensorflow.compat.v1 as tfc
import numpy as np
from datetime import datetime

np.random.seed(10)
tfc.set_random_seed(10)

# Line: y = W*X + b
W, b = 0.5, 1.4

# 100 item data sample set
X = np.linspace(0, 100, num=100)
# add random noise to y
y = np.random.normal(loc=W * X + b, scale=2.0, size=len(X))

# Tensorflow
gr = tf.Graph()
with gr.as_default():
    x_ph = tfc.placeholder(shape=[None, ], dtype=tf.float32)
    y_ph = tfc.placeholder(shape=[None, ], dtype=tf.float32)

    v_weight = tfc.get_variable("weight", shape=[1], dtype=tf.float32)
    v_bias = tfc.get_variable("bias", shape=[1], dtype=tf.float32)

    # Line computation
    out = v_weight * x_ph + v_bias
    # compute mean squared error
    loss = tf.reduce_mean((out - y_ph) ** 2)
    # minimize MSE loss
    opt = tfc.train.AdamOptimizer(0.4).minimize(loss)

    tf.summary.scalar('MSEloss', loss)
    tf.summary.histogram('model_weight', v_weight)
    tf.summary.histogram('model_bias', v_bias)

    # merge summary
    all_summary = tfc.summary.merge_all()

    # log summary to file
    now = datetime.now()
    clock_time = f'{now.day}_{now.hour}.{now.minute}.{now.second}'
    file_writer = tfc.summary.FileWriter('log_dir\\' + clock_time, tfc.get_default_graph())

    # create session
    session = tfc.Session(graph=gr)
    session.run(tfc.global_variables_initializer())

    # loop to train the parameters
    for ep in range(210):
        # run optimizer
        train_loss, _, train_summary = session.run([loss, opt, all_summary], feed_dict={x_ph: X, y_ph: y})
        file_writer.add_summary(train_summary, ep)

        # print epoch and loss
        if ep % 40 == 0:
            print(f'Epoch: {ep}'.ljust(13) + f'MSE: {train_loss:.4f}'.ljust(16) + f'W: {session.run(v_weight)[0]:.3f}'.ljust(11) + f'b: {session.run(v_bias)[0]:.3f}')

    print(f'Final weight: {session.run(v_weight)[0]:.3f},  bias: {session.run(v_bias)[0]:.3f}')
    file_writer.close()

session.close()

python tensorflow reinforcement-learning
1个回答
0
投票

请将model_weightmodel_bias摘要调用从tf更改为tfc

这里是修改后的代码

tfc.summary.histogram('model_weight', v_weight)
tfc.summary.histogram('model_bias', v_bias)

请参考下面的输出中的完整代码

import tensorflow as tf
import tensorflow.compat.v1 as tfc
import numpy as np
from datetime import datetime

np.random.seed(10)
tfc.set_random_seed(10)

# Line: y = W*X + b
W, b = 0.5, 1.4

# 100 item data sample set
X = np.linspace(0, 100, num=100)
# add random noise to y
y = np.random.normal(loc=W * X + b, scale=2.0, size=len(X))

# Tensorflow
gr = tf.Graph()
with gr.as_default():
    x_ph = tfc.placeholder(shape=[None, ], dtype=tf.float32)
    y_ph = tfc.placeholder(shape=[None, ], dtype=tf.float32)

    v_weight = tfc.get_variable("weight", shape=[1], dtype=tf.float32)
    v_bias = tfc.get_variable("bias", shape=[1], dtype=tf.float32)

    # Line computation
    out = v_weight * x_ph + v_bias
    # compute mean squared error
    loss = tf.reduce_mean((out - y_ph) ** 2)
    # minimize MSE loss
    opt = tfc.train.AdamOptimizer(0.4).minimize(loss)

    tf.summary.scalar('MSEloss', loss)
    tfc.summary.histogram('model_weight', v_weight)
    tfc.summary.histogram('model_bias', v_bias)

    # merge summary
    all_summary = tfc.summary.merge_all()

    # log summary to file
    now = datetime.now()
    clock_time = f'{now.day}_{now.hour}.{now.minute}.{now.second}'
    file_writer = tfc.summary.FileWriter('log_dir\\' + clock_time, tfc.get_default_graph())

    # create session
    session = tfc.Session(graph=gr)
    session.run(tfc.global_variables_initializer())

    # loop to train the parameters
    for ep in range(210):
        # run optimizer
        train_loss, _, train_summary = session.run([loss, opt, all_summary], feed_dict={x_ph: X, y_ph: y})
        file_writer.add_summary(train_summary, ep)

        # print epoch and loss
        if ep % 40 == 0:
            print(f'Epoch: {ep}'.ljust(13) + f'MSE: {train_loss:.4f}'.ljust(16) + f'W: {session.run(v_weight)[0]:.3f}'.ljust(11) + f'b: {session.run(v_bias)[0]:.3f}')

    print(f'Final weight: {session.run(v_weight)[0]:.3f},  bias: {session.run(v_bias)[0]:.3f}')
    file_writer.close()

session.close()

输出:

Epoch: 0     MSE: 2618.5493  W: 0.997   b: 0.158
Epoch: 40    MSE: 15.1722    W: 0.494   b: 0.108
Epoch: 80    MSE: 4.4201     W: 0.503   b: 0.709
Epoch: 120   MSE: 3.8206     W: 0.505   b: 1.200
Epoch: 160   MSE: 3.7423     W: 0.499   b: 1.514
Epoch: 200   MSE: 3.7225     W: 0.497   b: 1.687
Final weight: 0.496,  bias: 1.712

注意:此代码在Tensorflow 1.x和Tensorflow 2.x版本中均兼容,并且已在Google Colab中运行

© www.soinside.com 2019 - 2024. All rights reserved.