Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 自定义丢失问题:急切执行函数的输入不能作为符号张量,但可以找到_Python_Tensorflow_Keras - Fatal编程技术网

Python 自定义丢失问题:急切执行函数的输入不能作为符号张量,但可以找到

Python 自定义丢失问题:急切执行函数的输入不能作为符号张量,但可以找到,python,tensorflow,keras,Python,Tensorflow,Keras,我使用tensorflow keras自定义损失(使用其他输入数据计算损失)的代码如下: inp = Input(shape=(inp_seq_len,feature_size)) w = Input(shape=(1)) # code ommitted def custom_loss(w): def loss(y_true,y_pred): return -y_true*y_pred*w return loss model = Model( inputs

我使用tensorflow keras自定义损失(使用其他输入数据计算损失)的代码如下:

inp = Input(shape=(inp_seq_len,feature_size))
w = Input(shape=(1))
# code ommitted

def custom_loss(w):
    def loss(y_true,y_pred):
        return -y_true*y_pred*w
    return loss

model = Model(
  inputs=[inp, w],
  outputs=[pred],
)
model.compile(loss=custom_loss(w), optimizer='adam')
在执行model.fit()时,我遇到错误: 对急切执行函数的输入不能作为符号张量,但可以找到:tf.Tensor'input_19:0 shape=[None,1],dtype=float32

接下来,我做了: tf.config.experimental\u急切地运行函数(True)

然后我得到了一个错误: 无法将符号张量(truediv_20:0)转换为numpy数组

请注意,我遵循了更改代码的步骤

我的原始代码是(从一个inpA中解包inp和w):

我有/没有犯的错误 tf.config.experimental\u急切地运行函数(True) 同上

我能做什么

谢谢

更新:使用以下帖子中的代码时出现此错误:

WARNING:tensorflow:Output dense_3 missing from loss dictionary. We assume this was done on purpose. The fit and evaluate APIs will not be expecting any data to be passed to dense_3.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-1f23bd570780> in <module>()
     19 m.add_loss( mse( true, out, sample_weight ) )
     20 m.compile(loss=None, optimizer='adam')
---> 21 history = m.fit([X, y, W], y, epochs=10)
     22 
     23 # final fitted model to compute predictions

/nfs/anaconda2/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training.pyc in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    817         max_queue_size=max_queue_size,
    818         workers=workers,
--> 819         use_multiprocessing=use_multiprocessing)
    820 
    821   def evaluate(self,

/nfs/anaconda2/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training_v2.pyc in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    233           max_queue_size=max_queue_size,
    234           workers=workers,
--> 235           use_multiprocessing=use_multiprocessing)
    236 
    237       total_samples = _get_total_number_of_samples(training_data_adapter)

/nfs/anaconda2/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training_v2.pyc in _process_training_inputs(model, x, y, batch_size, epochs, sample_weights, class_weights, steps_per_epoch, validation_split, validation_data, validation_steps, shuffle, distribution_strategy, max_queue_size, workers, use_multiprocessing)
    591         max_queue_size=max_queue_size,
    592         workers=workers,
--> 593         use_multiprocessing=use_multiprocessing)
    594     val_adapter = None
    595     if validation_data:

/nfs/anaconda2/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training_v2.pyc in _process_inputs(model, mode, x, y, batch_size, epochs, sample_weights, class_weights, shuffle, steps, distribution_strategy, max_queue_size, workers, use_multiprocessing)
    644     standardize_function = None
    645     x, y, sample_weights = standardize(
--> 646         x, y, sample_weight=sample_weights)
    647   elif adapter_cls is data_adapter.ListsOfScalarsDataAdapter:
    648     standardize_function = standardize

/nfs/anaconda2/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training.pyc in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset)
   2381         is_dataset=is_dataset,
   2382         class_weight=class_weight,
-> 2383         batch_size=batch_size)
   2384 
   2385   def _standardize_tensors(self, x, y, sample_weight, run_eagerly, dict_inputs,

/nfs/anaconda2/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training.pyc in _standardize_tensors(self, x, y, sample_weight, run_eagerly, dict_inputs, is_dataset, class_weight, batch_size)
   2467           shapes=None,
   2468           check_batch_axis=False,  # Don't enforce the batch size.
-> 2469           exception_prefix='target')
   2470 
   2471       # Generate sample-wise weight values given the `sample_weight` and

/nfs/anaconda2/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training_utils.pyc in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    494       raise ValueError(
    495           'Error when checking model ' + exception_prefix + ': '
--> 496           'expected no data, but got:', data)
    497     return []
    498   if data is None:

ValueError: ('Error when checking model target: expected no data, but got:', array([3.39102071e-01, 1.23122638e-01, 7.54209531e-01, 8.10110230e-01,
警告:tensorflow:丢失字典中缺少输出密集_3。我们认为这是故意的。fit和evaluate API不希望任何数据传递给dense_3。
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
19 m.添加损耗(mse(真实、输出、样品重量))
20 m.compile(loss=None,optimizer='adam')
--->21历史=m.fit([X,y,W],y,历代=10)
22
23#用于计算预测的最终拟合模型
/nfs/anaconda2/lib/python2.7/site-packages/tensorflow\u core/python/keras/engine/training.pyc in fit(self、x、y、批大小、历元、冗余、回调、验证拆分、验证数据、洗牌、类权重、样本权重、初始历元、每历元步数、验证步骤、验证频率、最大队列大小、工作人员、使用多处理、**kwargs)
817最大队列大小=最大队列大小,
818名工人=工人,
-->819使用\多处理=使用\多处理)
820
821 def评估(自我,
/nfs/anaconda2/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training_v2.pyc in-fit(self、model、x、y、批大小、历元、冗余、回调、验证拆分、验证数据、洗牌、类权重、样本权重、初始历元、每个历元的步骤、验证步骤、验证频率、最大队列大小、工作人员、使用多处理、**kwargs)
233最大队列大小=最大队列大小,
234名工人=工人,
-->235使用多处理=使用多处理)
236
237总样本数=\u获取\u总样本数\u(训练\u数据\u适配器)
/nfs/anaconda2/lib/python2.7/site-packages/tensorflow\u core/python/keras/engine/training\u v2.pyc进程内\u训练\u输入(模型、x、y、批次大小、历元、样本权重、类权重、每历元步长、验证拆分、验证数据、验证步骤、洗牌、分布策略、最大队列大小、工人、使用多处理)
591最大队列大小=最大队列大小,
592名工人=工人,
-->593使用多处理=使用多处理)
594 val_适配器=无
595如果验证数据:
/nfs/anaconda2/lib/python2.7/site-packages/tensorflow\u core/python/keras/engine/training\u v2.pyc进程内输入(模型、模式、x、y、批次大小、时代、样本权重、类权重、无序、步骤、分布策略、最大队列大小、工作者、使用多进程)
644标准化功能=无
645 x,y,样本重量=标准化(
-->646 x,y,样本重量=样本重量)
647 elif适配器\u cls是数据\u适配器。列表ScalarsDataAdapter:
648标准化功能=标准化
/nfs/anaconda2/lib/python2.7/site-packages/tensorflow\u core/python/keras/engine/training.pyc在标准化用户数据中(self、x、y、样本权重、类权重、批量大小、检查步骤、步骤名称、步骤、验证、拆分、洗牌、从数据集中提取张量)
2381 is_数据集=is_数据集,
2382级重量=级重量,
->2383批次大小=批次大小)
2384
2385定义标准化张量(自、x、y、样本重量、急切运行、dict输入、,
/nfs/anaconda2/lib/python2.7/site-packages/tensorflow\u core/python/keras/engine/training.pyc中的标准化张量(self、x、y、sample\u weight、run\u热切、dict\u输入、is\u数据集、类权重、批量大小)
2467个形状=无,
2468 check_batch_axis=False,#不强制执行批大小。
->2469例外(前缀为“目标”)
2470
2471#根据“样本权重”生成样本权重值,以及
/标准化输入数据中的nfs/anaconda2/lib/python2.7/site-packages/tensorflow\u core/python/keras/engine/training\u utils.pyc(数据、名称、形状、检查批处理轴、异常前缀)
494升值错误(
495“检查模型“+异常前缀+”时出错::”
-->496'预期没有数据,但得到:',数据)
497返回[]
498如果数据为无:
ValueError:(“检查模型目标时出错:应无数据,但已获取:”,数组([3.39102071e-01,1.23122638e-01,7.54209531e-01,8.10110230e-01,

这是一种将附加参数传递给自定义损失函数(在您的例子中是一个权重数组)的变通方法。诀窍在于使用假输入,这有助于以正确的方式构建和使用损失。不要忘记keras处理固定的批次维度

我提供了一个回归问题中的虚拟示例,可以根据您的需要轻松修改

def mse(y_true, y_pred, sample_weight):

    error = y_true-y_pred

    return K.mean(K.sqrt(error)*sample_weight)


X = np.random.uniform(0,1, (1000,10))
y = np.random.uniform(0,1, 1000)
W = np.random.uniform(1,2, 1000)

inp = Input((10,))
true = Input((1,))
sample_weight = Input((1,))
x = Dense(32, activation='relu')(inp)
out = Dense(1)(x)

m = Model([inp,true, sample_weight], out)
m.add_loss( mse( true, out, sample_weight ) )
m.compile(loss=None, optimizer='adam')
history = m.fit([X, y, W], y, epochs=10)

# final fitted model to compute predictions
final_m = Model(inp, out)
def mse(y_true, y_pred, sample_weight):

    error = y_true-y_pred

    return K.mean(K.sqrt(error)*sample_weight)


X = np.random.uniform(0,1, (1000,10))
y = np.random.uniform(0,1, 1000)
W = np.random.uniform(1,2, 1000)

inp = Input((10,))
true = Input((1,))
sample_weight = Input((1,))
x = Dense(32, activation='relu')(inp)
out = Dense(1)(x)

m = Model([inp,true, sample_weight], out)
m.add_loss( mse( true, out, sample_weight ) )
m.compile(loss=None, optimizer='adam')
history = m.fit([X, y, W], y, epochs=10)

# final fitted model to compute predictions
final_m = Model(inp, out)