当启用急切执行时,传递给 Optimizer.compute_gradients 的 `loss` 应该是一个函数 [英] `loss` passed to Optimizer.compute_gradients should be a function when eager execution is enabled

查看:27
本文介绍了当启用急切执行时,传递给 Optimizer.compute_gradients 的 `loss` 应该是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 TensorFlow 的新手,我刚刚开始学习和理解它.我正在研究 neural style transfer 问题,我正在使用 tensorflow version 1.14.

I am new to TensorFlow, I just started learning and understanding it. I am working on neural style transfer problem and I am using tensorflow version 1.14.

我收到一个错误传递给 Optimizer.compute_gradients 的损失应该是启用急切执行时的函数.

我试图通过使用 TensorFlow 图而不是 eager execution 来解决这个问题,但它不起作用.我想使用 eager execution 因为它看起来更像 pythonic 方式.

I tried to solve the problem by using TensorFlow graph instead of eager execution, but it's not working. I want to use eager execution because it looks like a more pythonic way.

这是我的代码,抱歉把整个代码放在这里,请在我的代码中提出更正建议.

here is my code, sorry for putting whole code here, please suggest corrections in my code.

import scipy
import tensorflow as tf
import tensorflow.contrib.eager as tfe
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from scipy import misc
from skimage.transform import resize
from tensorflow.keras.applications.vgg19 import VGG19, preprocess_input
from tensorflow.keras import backend as K

tf.enable_eager_execution()
print('Eager execution {}'.format(tf.executing_eagerly()))

content_path = '800px-Green_Sea_Turtle_grazing_seagrass.jpg'
style_path = '800px-The_Great_Wave_off_Kanagawa.jpg'

content_img = plt.imread(content_path)
plt.imshow(content_img)
style_img = plt.imread(style_path)
plt.imshow(style_img)

MEANS = np.array([123.68, 116.779, 103.939]).reshape((1,1,1,3))
content_img = resize(content_img, (552,800,3)) #resized content img because style img has shape (552,800,3)

content_img = np.array(content_img)
content_img = np.reshape(content_img, ((1,)+content_img.shape))
style_img = np.array(style_img)
style_img = np.reshape(style_img, ((1,)+style_img.shape))

noise_img= np.random.uniform(-20,20,(1,552,800,3)).astype('float32')
generated_img = noise_img*0.6 + content_img*0.4
plt.imshow(generated_img[0])

content_img = content_img-MEANS
style_img = style_img-MEANS

model = VGG19(include_top=False, weights='imagenet')

def compute_content_cost(act_content_img, act_generated_img):
    return tf.reduce_mean(tf.square(act_content_img-act_generated_img))

def gram_matrix(A):
    gram = tf.matmul(A, tf.transpose(A))
    return gram

def style_loss_one_layer(act_style_img, act_generated_img):
    m,n_H,n_W,n_C = tf.shape(act_generated_img)               #act_generated_img.get_shape().as_list()
    gram_act_style_img = gram_matrix(act_style_img)
    gram_generated_img = gram_matrix(act_generated_img)
    return tf.reduce_mean(tf.square(gram_act_style_img-gram_generated_img))*(1/(4*n_C**2*(n_H*n_W)**2))

content_layer = ['block5_conv2']
style_layers = [('block1_conv1',0.2), 
                ('block2_conv1',0.2),
                ('block3_conv1',0.2),
                ('block4_conv1',0.2),
                ('block5_conv1',0.2)]

def compute_style_cost(model, style_layers):
    style_cost = total_style_cost = 0
    for layer, coeff in style_layers:
        act_style_img = model.get_layer(layer).output
        act_generated_img = model.get_layer(layer).output
        style_cost += style_loss_one_layer(act_style_img, act_generated_img)
        total_style_cost += coeff*style_cost 
    return total_style_cost

def compute_total_cost(J_content, J_style, alpha=10, beta=40):
    J = (alpha*tf.cast(J_content, tf.float64)) + (beta*J_style)
    return J

act_generated_img = model.get_layer('block5_conv2').output
act_content_img = model.get_layer('block5_conv2').output

J_content = compute_content_cost(act_content_img=act_content_img, act_generated_img=act_generated_img)
print(J_content)
J_style = compute_style_cost(model, style_layers=style_layers)
print(J_style)

J_total_cost = compute_total_cost(J_content, J_style, alpha=10, beta=40)
print(J_total_cost)

optimizer = tf.train.AdamOptimizer(2.0)

train_step = optimizer.minimize(J_total_cost)        #**getting error here**

推荐答案

上述错误主要是当您尝试使用 TensorFlow 1.x 但系统运行的是 tensor 2.0 时引起的.

The above error is mainly caused when you are trying to use TensorFlow 1.x but the system is running tensor 2.0.

使用以下代码初始化 TensorFlow,以确保您尝试使用 1.0 版

Initialise the TensorFlow using the code below to ensure you are trying to use the version 1.0

将 tensorflow.compat.v1 导入为 tf

import tensorflow.compat.v1 as tf

您可以在初始化程序之后通过以下命令使系统禁用该行为.

You can make the system disable that behaviour by the below command after the initialisers.

tf.disable_v2_behavior()

tf.disable_v2_behavior()

这篇关于当启用急切执行时,传递给 Optimizer.compute_gradients 的 `loss` 应该是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆