Tensorflow学习

环境

tensorflow 2.3

报错与问题复现

AttributeError: ‘Dense’ object has no attribute ‘op’

问题复现
import tensorflow as tf
import numpy as np
from tensorflow.keras.layers import Input,Dense
from tensorflow.keras.models import Model

s_dim=3
a_dim=1

Input_layer_s=Input(shape=s_dim,name='C_input_s')
Input_layer_a=Input(shape=a_dim,name='C_input_a')
Input_critic_x=tf.concat([Input_layer_s,Input_layer_a],1)
inv_l1=Dense(units=60,activation='relu',name='inv_critic_layer1')(Input_critic_x)
out_put=Dense(units=1,name='C_out')(inv_l1)
model=Model(inputs=[Input_layer_s,Input_layer_a],outputs=out_put,name='critic')
原因分析

主要是inv_l1类型为Dense层,导致out_put无法转化

解决办法

如果inv_l1一开始就是Tensor类型的话就不会报错,实现方法就是inv_l1继承Input_critic_x的类型。改为

import tensorflow as tf
import numpy as np
from tensorflow.keras.layers import Input,Dense
from tensorflow.keras.models import Model

s_dim=3
a_dim=1

Input_layer_s=Input(shape=s_dim,name='C_input_s')
Input_layer_a=Input(shape=a_dim,name='C_input_a')
x=tf.concat([Input_layer_s,Input_layer_a],1)
x=Dense(units=60,activation='relu',name='inv_critic_layer1')(x)
x=Dense(units=1,name='C_out')(x)
model=Model(inputs=[Input_layer_s,Input_layer_a],outputs=x,name='critic')

TypeError: An op outside of the function building code is being passed

a “Graph” tensor.
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
TypeError: An op outside of the function building code is being passed
a “Graph” tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
@tf.function
def has_init_scope():
my_constant = tf.constant(1.)
with tf.init_scope():
added = my_constant * 2
The graph tensor has name: Exp_1:0

问题复现
    @tf.function
    def update_actor(self,bs):
        with tf.GradientTape(persistent= True) as tape:
            action, log_prob, z, mu, log_std = self.evaluation(bs)
            Q_value_1       = self.q_value_1([bs,action])
            Q_value_2       = self.q_value_2([bs,action])
            Q_min           = tf.math.minimum(Q_value_1,Q_value_2)
            Policy_loss     = tf.reduce_mean(self.alpha*log_prob-Q_min)
            alpha_loss      = tf.reduce_mean(-(self.log_alpha *(log_prob + self.alpha_entropy)))
        Policy_grads = tape.gradient(Policy_loss, self.actor.trainable_weights) # 得到偏LOSS偏omega
        Policy_grads = [tf.clip_by_norm(g, 0.5) for g in Policy_grads]
        self.actor_opt.apply_gradients(zip(Policy_grads, self.actor.trainable_weights))# 进行一次梯度下降(目的是降低LOSS)
        self.alpha_temp_opt.minimize(alpha_loss,[self.log_alpha],tape = tape)
        self.alpha = tf.math.exp(self.log_alpha)
        del tape
原因分析

主要是因为self.alpha = tf.math.exp(self.log_alpha)这一句的问题,因为在graph模式下self.log_aplha将变得难以观测,所以self.alpha的值就不正常,然后再次使用这个函数的时候self.alpha就彻底完蛋了,graph模式就认为 Graph tensors leak out了
下面这个程序运行后就会发现: A.alpha 的输出为Tensor(“Exp:0”, shape=(), dtype=float32),而你不用graph模式(注释掉tf.function),就能正常有值
尝试下注释掉self.run 中的self.update1,转用剩下的两行。

# test
import tensorflow as tf
class test(object):
    def __init__(self) -> None:
        self.alpha = tf.Variable(0.2)
        self.log_alpha = tf.Variable(tf.math.log(self.alpha))
        self.opt = tf.keras.optimizers.Adam(learning_rate=0.1)
        self.entropy = tf.constant(-6.)
    
    @tf.function
    def update(self):
        with tf.GradientTape() as tape:
            loss = tf.reduce_mean(self.log_alpha * self.log_alpha*self.entropy)
        self.opt.minimize(loss,[self.log_alpha],tape = tape)
        
    
    @tf.function
    def update1(self):
        with tf.GradientTape() as tape:
            loss = tf.reduce_mean(self.log_alpha * self.log_alpha*self.entropy)
        self.opt.minimize(loss,[self.log_alpha],tape = tape)
        self.alpha = tf.math.exp(self.log_alpha)

    def run(self) :
        # self.update()
        # self.alpha = tf.math.exp(self.log_alpha)

        self.update1()

A = test()
print(A.alpha)
print(A.log_alpha.numpy())
A.run()
print(A.alpha)
print(A.log_alpha.numpy())
A.run()
print(A.alpha)
print(A.log_alpha.numpy())

matlab 安装

安装matlab.engine的时候,按照说明安装,结果竟然报错
SetuptoolsDeprecationWarning: setup.py install is deprecated
这是因为setuptools版本太高,退一个版本就行了

pip install setuptools==58.2.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值