我需要将传递给我的自定义丢失函数的Tensorflow张量转换为numpy数组,进行一些更改并将其转换回张量。我读了一些建议在调用tensorflow会话后使用eval()函数的答案,但是我需要在loss函数中进行这种转换。我应该怎么做呢?
def loss_fn(y_true, y_pred):
#obtain y_true_np and y_pred_np - How to do this?
#make changes to array
#convert back using backend.variable()
编辑:试过以下代码:
def loss_fn(y_true, y_pred):
#obtain y_true_np and y_pred_np - How to do this?
y_true_np = K.eval(y_true)
y_pred_np = K.eval(y_pred)
# make changes to numpy arrays
# obtain modified numpy arrays as y_true_mod & y_pred_mod
# convert back to tensors using backend.variable()
out_true = K.variable(y_true_mod)
out_pred = K.variable(y_pred_mod)
# continue to define your custom loss with out_true & out_pred...
但是我在以下行中收到错误:
y_true_np = K.eval(y_true)
错误消息:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'out_layer_target' with dtype float
[[Node: out_layer_target = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
其中out_layer是我最终卷积层的名称(也是输出层)
EDIT2:我为这种困惑道歉
我正在从头开始训练一个完全卷积的模型
输入尺寸 - 720x560x1
输出尺寸 - 640x480x3
所以我的代码如下:
#Define loss function as above
def loss_fn(y_true, y_pred):
#obtain y_true_np and y_pred_np - How to do this?
y_true_np = K.eval(y_true)
y_pred_np = K.eval(y_pred)
# make changes to numpy arrays
# obtain modified numpy arrays as y_true_mod & y_pred_mod
# convert back to tensors using backend.variable()
out_true = K.variable(y_true_mod)
out_pred = K.variable(y_pred_mod)
# continue to define your custom loss with out_true & out_pred
#return loss calculated using the tensors
#main
#define model
input_layer = Input(shape = (None, None, 1), name='input_layer')
x = something()(input_layer)
...
out_layer = Conv2D(3, (9, 9), strides = 1, padding = 'same', activation = 'tanh', name='out_layer', kernel_initializer=weights_init)(x)
model = Model(input = [input_layer], output = [out_layer])
model.compile(Adam(lr=1e-3), loss = [loss_fn], metrics=['accuracy'])
print(model.summary())
因此错误实际上是在model.compile()期间,这是由函数loss_fn中的以下行引起的:
y_true_np = K.eval(y_true)
我还没有开始使用model.fit()进行训练,因此没有训练数据输入内存。我想检查模型是否编译0错误。我希望这些信息有所帮助。
错误讯息:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'out_layer_target' with dtype float
[[Node: out_layer_target = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
EDIT3:
这是我编写的一小段代码,可以重现以获得相同的错误。
import numpy as np
import cv2
import tensorflow as tf
import keras
from keras.models import *
from keras.layers import *
from keras.optimizers import SGD, Adam
from keras import backend as K
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
K.set_session(sess)
def loss_fn(y_true, y_pred):
y_true_np = K.eval(y_true) #y_true.eval(session=sess)
loss = K.Variable(0.)
return loss
input_layer = Input(shape = (560, 720, 1), name='input_layer')
x = BatchNormalization(axis=-1, name='BN')(input_layer)
out_layer = Conv2D(3, (9, 9), strides = 1, padding = 'same', activation = 'tanh', name='out_layer')(x)
model = Model(inputs=[input_layer], outputs=[out_layer])
print(model.summary())
model.compile(Adam(lr=1e-3), loss = [loss_fn], metrics=['accuracy'])
我也对这个简单的模型也犯了同样的错误。您可以使用此整个代码段重现错误。