嗨,我正在使用keras中的自定义损失函数进行编译,但是会引发错误
如果Y_test和Y_pred是二进制分类变量 比我必须为Y_test和Y_pred创建哑变量。 这里需要将张量对象转换为Numpy数组。
以下是自定义功能
def keras_custom_loss_function(Y_true, Y_pred):
#convert Y_test and Y_pred into Numpy arrays
Y_pred = np.array(Y_pred)
Y_test = np.array(Y_test)
#create Dummy variable for Y_test and Y_pred
en_Y_test = pd.get_dummies(Y_test)
en_Y_pred = pd.get_dummies(Y_pred)
...
# DO further operations
#在编译过程中,它抛出以下错误
model.compile(loss=keras_custom_loss_function, optimizer=opt, metrics=['accuracy'])
TypeError:无法遍历具有未知第一维的张量。
请帮助如何从通过编译函数生成的张量对象(Y_test,Y_pred)创建numpy数组
提前感谢
修改:1 我们正在尝试计算损失函数中的以下逻辑 以下是示例 假设Y_test和Y_pred值来自keras编译函数,
Y_true = pd.Series([2,1,2,2,1,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1]
Y_pred = pd.Series([2,2,2,1,1,2,2,2,1,1,2,2,2,2,2,2,2,2,2,2]
比具有参数Y_test,Y_Pred的自定义损失函数
def keras_custom_loss_function(Y_true, Y_pred):
#convert Y_test and Y_pred into Numpy arrays
Y_pred = np.array(Y_pred)
Y_test = np.array(Y_test)
#create Dummy variable for Y_test and Y_pred
en_Y_test = pd.get_dummies(Y_test)
en_Y_pred = pd.get_dummies(Y_pred)
# DO further operations
#Change the column names
en_Y_true.columns = ["D1","D2"]
en_Y_pred.columns = ["Y1","Y2"]
#Create dataframe and append
Custm_df = pd.concat([en_Y_true, en_Y_pred], axis=1)
#convert datatypes
Custm_df['D1'] = Custm_df['D1'].astype('int32')
Custm_df['D2'] = Custm_df['D2'].astype('int32')
Custm_df['Y1'] = Custm_df['Y1'].astype('int32')
Custm_df['Y2'] = Custm_df['Y2'].astype('int32')
Custm_df['D1-Y1'] = Custm_df['D1'] -Custm_df['Y1']
Custm_df['D2-Y2'] = Custm_df['D2'] -Custm_df['Y2']
loss = Formula based on (D1,D2,Y1,Y2)
return loss
我在编译完成时遇到错误
TypeError Traceback (most recent call last)
<ipython-input-22-22841b8add1e> in <module>
26
27 #model.compile(loss=custom_loss, optimizer=opt, metrics=['accuracy']) # loss 'adam' ,y_predicted
---> 28 model.compile(loss=keras_custom_loss_function, optimizer=opt, metrics=['accuracy']) # loss 'adam' ,y_predicted
~\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
227 # loss_weight_2 * output_2_loss_fn(...) +
228 # layer losses.
--> 229 self.total_loss = self._prepare_total_loss(masks)
230
231 # Functions for train, test and predict will
~\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\training.py in _prepare_total_loss(self, masks)
690
691 output_loss = loss_fn(
--> 692 y_true, y_pred, sample_weight=sample_weight)
693
694 if len(self.outputs) > 1:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\losses.py in __call__(self, y_true, y_pred, sample_weight)
69 scope_name = 'lambda' if self.name == '<lambda>' else self.name
70 with K.name_scope(scope_name):
---> 71 losses = self.call(y_true, y_pred)
72 return losses_utils.compute_weighted_loss(
73 losses, sample_weight, reduction=self.reduction)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\losses.py in call(self, y_true, y_pred)
130 Loss values per sample.
131 """
--> 132 return self.fn(y_true, y_pred, **self._fn_kwargs)
133
134 def get_config(self):
<ipython-input-21-e80cbe1c715d> in keras_custom_loss_function(Y_true, Y_pred)
39
40 #label_numpy = Y_pred.eval()
---> 41 en_Y_pred = pd.get_dummies(Y_pred)
42 print("second after")
43 en_Y_true.columns = ["D1","D2"]
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in get_dummies(data, prefix, prefix_sep, dummy_na, columns, sparse, drop_first, dtype)
864 sparse=sparse,
865 drop_first=drop_first,
--> 866 dtype=dtype)
867 return result
868
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in _get_dummies_1d(data, prefix, prefix_sep, dummy_na, sparse, drop_first, dtype)
872 from pandas.core.reshape.concat import concat
873 # Series avoids inconsistent NaN handling
--> 874 codes, levels = _factorize_from_iterable(Series(data))
875
876 if dtype is None:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in __init__(self, data, index, dtype, name, copy, fastpath)
227 elif (isinstance(data, compat.Iterable)
228 and not isinstance(data, compat.Sized)):
--> 229 data = list(data)
230 else:
231
~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in __iter__(self)
484 if shape[0] is None:
485 raise TypeError(
--> 486 "Cannot iterate over a tensor with unknown first dimension.")
487 for i in xrange(shape[0]):
488 yield self[i]
TypeError: Cannot iterate over a tensor with unknown first dimension.