AdamOptimizer的问题

时间:2019-05-02 13:15:01

标签: python-3.x tensorflow tensorflow2.0

我正在使用一个简单的网络,并且尝试使用 AdamOptimizer 以最小化Q学习环境中的损失

代码在这里:

### DATASET IMPORT
from DataSet import *

### NETWORK
state_size      = STATE_SIZE
stack_size      = STACK_SIZE
action_size     = ACTION_SIZE
learning_rate   = LEARNING_RATE
hidden_tensors  = HIDDEN_TENSORS
gamma           = GAMMA

import tensorflow as tf
import numpy as np

class NNetwork:       
    def __init__(self, name='NNetwork'):

        # Initialisations
        self.state_size     = state_size
        self.action_size    = action_size
        self.model          = tf.keras.models.Sequential()
        self.optimizer      = tf.keras.optimizers.Adam(learning_rate)

        # Network shaping
        self.model.add(tf.keras.layers.Dense(self.state_size,   activation='relu',      kernel_initializer='glorot_uniform'))
        self.model.add(tf.keras.layers.Dense(hidden_tensors,    activation='relu',      kernel_initializer='glorot_uniform'))
        self.model.add(tf.keras.layers.Dense(action_size,       activation='linear',    kernel_initializer='glorot_uniform'))

    # Prediction function (return Q_values)
    def get_outputs(self, inputs):
        inputs = tf.convert_to_tensor(inputs, dtype=tf.float32)
        return self.model.predict(inputs)

    # Optimization of the network
    def optimize(self, state, action, reward, next_state):
        next_Q_values   = self.get_outputs(next_state)
        target_Q        = reward + gamma * np.max(next_Q_values)
        curent_Q        = tf.reduce_sum(tf.multiply(self.get_outputs(state), action))
        loss           = tf.square(target_Q - curent_Q)
        self.optimizer.minimize(tf.convert_to_tensor(loss), self.model.trainable_variables)



B = NNetwork('b')
print(B.get_outputs([[0.12, 0.59]]))

B.optimize([[0.12, 0.59]], [1, 0, 0, 0, 0, 0, 0], 100000000, [[0.13, 0.58]])
print(B.get_outputs([[0.12, 0.59]]))

所以我的问题是:

当我执行此代码时,我得到了:

  

[[-0.00105272 0.02356465 -0.01908724 -0.03868931 0.01585
  0.02427034      [0.00203115]]追溯(最近一次通话最后一次):文件“。\ DQNet.py”,第69行,在       B.optimize([[[0.12,0.59]],[1、0、0、0、0、0、0],100000000,[[0.13、0.58]])文件“。\ DQNet.py”,第62行,在优化中       tf.keras.optimizers.Adam(learning_rate).minimize(tf.convert_to_tensor(10),   self.model.trainable_variables)文件“ C:\ Users \ Odeven poste   1 \ Documents [Python-3.6.8 \ python-3.6.8.amd64 \ lib \ site-packages \ tensorflow \ python \ keras \ optimizer_v2 \ optimizer_v2.py“,   第296行,最小化       损失,var_list = var_list,grad_loss = grad_loss)文件“ C:\ Users \ Odeven poste   1 \ Documents [Python-3.6.8 \ python-3.6.8.amd64 \ lib \ site-packages \ tensorflow \ python \ keras \ optimizer_v2 \ optimizer_v2.py“,   _compute_gradients中的第328行       loss_value = loss()TypeError:'tensorflow.python.framework.ops.EagerTensor'对象不可调用

这意味着我的网络可以正常运行,因为我得到了Q值,但是当我尝试调用“优化”函数时,我在网上遇到了错误:

self.optimizer.minimize(tf.convert_to_tensor(loss), self.model.trainable_variables)

我真的不明白为什么会出现这个错误:

  

'tensorflow.python.framework.ops.EagerTensor'对象不可调用

因为我非常确定我必须赋予最小化函数的'损失'参数应该是张量...

1 个答案:

答案 0 :(得分:0)

在TF2中,最小化方法的loss参数必须是可调用的Python。

因此,您可以将损失定义更改为:

def loss():
    return tf.square(target_Q - curent_Q)

并在不将其转换为张量的情况下使用它:

self.optimizer.minimize(loss, self.model.trainable_variables)