我有简单的seq2seq模型:
import seq2seq
import numpy as np
import keras.backend as K
from seq2seq.models import Seq2Seq
from keras.models import Model
from keras.models import Sequential
from keras.layers import Embedding, Input, TimeDistributed, Activation
BLOCK_LEN = 60
EVENTS_CNT = 462
input = Input((BLOCK_LEN,))
embedded = Embedding(input_dim=EVENTS_CNT+1, output_dim=200)(input)
emb_model = Model(input, embedded)
seq_model = Seq2Seq(batch_input_shape=(None, BLOCK_LEN, 200), hidden_dim=200, output_length=BLOCK_LEN, output_dim=EVENTS_CNT)
model = Sequential()
model.add(emb_model)
model.add(seq_model)
model.add(TimeDistributed(Activation('softmax')))
model.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
model_1 (Model) (None, 60, 200) 92600
_________________________________________________________________
model_12 (Model) (None, 60, 462) 1077124
_________________________________________________________________
time_distributed_2 (TimeDist (None, 60, 462) 0
=================================================================
Total params: 1,169,724
Trainable params: 1,169,724
Non-trainable params: 0
_________________________________________________________________
我正在尝试创建自己的指标:
def symbol_acc(true, predicted):
np_y_true = K.get_value(true)
np_y_pred = K.get_value(predicted)
return K.mean(np_y_true == np_y_pred)
如果我尝试使用此指标编译模型,我会收到错误“您必须为占位符张量提供值”,并显示以下消息:
InvalidArgumentError Traceback (most recent call last)
C:\Users\Anna\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1322 try:
-> 1323 return fn(*args)
1324 except errors.OpError as e:
C:\Users\Anna\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
1301 feed_dict, fetch_list, target_list,
-> 1302 status, run_metadata)
1303
C:\Users\Anna\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
472 compat.as_text(c_api.TF_Message(self.status.status)),
--> 473 c_api.TF_GetCode(self.status.status))
474 # Delete the underlying status object from memory otherwise it stays alive
InvalidArgumentError: You must feed a value for placeholder tensor 'time_distributed_2_target' with dtype float and shape [?,?,?]
[[Node: time_distributed_2_target = Placeholder[dtype=DT_FLOAT, shape=[?,?,?], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
但是下面的代码工作正常(不会产生任何异常):
def symbol_acc2(true, predicted):
true = np.array(true)
predicted = np.array(predicted)
return K.variable((true == predicted).mean())
你能解释一下这个例外是什么意思吗?我认为symbol_acc和symbol_acc2正在做同样的事情.我是NN和keras的新手,所以也许我没有看到一些明显的东西.我在stackoverflow上看到了类似的问题,但没有找到适合我情况的答案.
最佳答案 指标,损失和整个模型是“象征性”的张量.
这意味着,在您开始拟合或预测之前,它们绝对没有数据(或值).
当你调用K.get_value时,你试图得到一个不存在的值. (它只会在您向模型“提供数据”时存在.它所谓的占位符是一个空输入张量,期望在拟合或预测时接收数据).
解决问题的方法就是不试图获取值. (numpy版本也不起作用,编译此函数时该值不存在).
您必须将所有操作保持为符号,并且在您提供数据时将执行这些操作.
所以:
def symbol_acc(true, predicted):
isEqual = K.cast(K.equal(true,predicted),K.floatx())
return K.mean(isEqual)