前提・実現したいこと
自作の損失関数でkerasによる機械学習を行いたいです。
まず行いたい機械学習について、22次元の数値から、2次元の数値を予測する回帰モデルです。
そして損失関数の内容については、出力の一つ目をA,二つ目をBとしたとき、A+B/nという式を考え、nの範囲243から600までの和
Σ(A+B/n)〔243..600〕
について、正解ラベルと予測結果の平均二乗誤差を出すというものです。
発生している問題・エラーメッセージ
損失関数を作成し、C:\Users(ユーザー名)\Anaconda3\Lib\site-packages\kerasに存在する、losses.pyのファイルに、自作の損失関数originallossを入力し、動くかどうかを確認しようとしたのですが、originallossという損失関数は存在しないというエラーが返ってきました。どのようにすればoriginallossが認識されるようになるのか知りたいです。
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-62-e841daab48c1> in <module> 64 model.add(Dense(2)) 65 model.compile(loss='oriloss', ---> 66 optimizer='adam') 67 68 hist1 = model.fit(X_data_std, y_data_std, batch_size=69, epochs=8249, verbose=1) ~\Anaconda3\lib\site-packages\keras\engine\training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs) 137 loss_functions = [losses.get(l) for l in loss] 138 else: --> 139 loss_function = losses.get(loss) 140 loss_functions = [loss_function for _ in range(len(self.outputs))] 141 self.loss_functions = loss_functions ~\Anaconda3\lib\site-packages\keras\losses.py in get(identifier) 131 # Arguments 132 identifier: None or str, name of the function. --> 133 134 # Returns 135 The loss function or None if `identifier` is None. ~\Anaconda3\lib\site-packages\keras\losses.py in deserialize(name, custom_objects) 112 msle = MSLE = mean_squared_logarithmic_error 113 kld = KLD = kullback_leibler_divergence --> 114 cosine = cosine_proximity 115 116 ~\Anaconda3\lib\site-packages\keras\utils\generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name) 163 if fn is None: 164 raise ValueError('Unknown ' + printable_module_name + --> 165 ':' + function_name) 166 return fn 167 else: ValueError: Unknown loss function:oriloss
該当のソースコード
学習用のコード
python
# -*- coding: utf_8 -*- import os import time import numpy as np np.set_printoptions(threshold=np.inf) from sklearn.preprocessing import StandardScaler # 標準化の計算 from keras.models import Sequential from keras.layers import Dense, Activation # ---------標準化準備----------------------- x_stdsc = StandardScaler() y_stdsc = StandardScaler() # ---------重み保存-------------------------- result_dir = 'result' if not os.path.exists(result_dir): os.mkdir(result_dir) # ------------CSV読み込み-------------------- data = np.loadtxt("train0.csv",delimiter=",") # test1 = np.loadtxt("train100.csv",delimiter=",") # ---------------xとyを割り当て----------------- X_data = data[:,:22] # y_data = data[:,[21,22]] y_data = data[:,22:] # X_test1 = test1[:,:6] # y_test1 = test1[:,[10, 11, 16]] # -----------------標準化------------------- # print(y_test) X_data_std = x_stdsc.fit_transform(X_data) # X_test_std1 = x_stdsc.transform(X_test1) y_data_std = y_stdsc.fit_transform(y_data) # y_test_std1 = y_stdsc.transform(y_test1) # ---------------モデルkeras------------------- model = Sequential() model.add(Dense(32, input_dim=22, init='uniform')) model.add(Activation('sigmoid')) model.add(Dense(213)) model.add(Activation('sigmoid')) model.add(Dense(2)) model.compile(loss='originalloss', optimizer='adam') hist1 = model.fit(X_data_std, y_data_std, batch_size=69, epochs=8249, verbose=1) # -------------------save model------------------- json_string = model.to_json() open(os.path.join(result_dir, 'model.json'), 'w').write(json_string) # -------------------save weights---------------------- model.save_weights(os.path.join(result_dir, 'weights.h5'))
実装しようとしている損失関数
python
def originalloss(y_true,y_pred): def f(n): return y_pred[:,0]+y_pred[:,1]/n def g(n): return y_true[:,0]+y_true[:,1]/n def sigma(func, frm, to): result = 0; #答えの受け皿 for i in range(frm, to): result += func(i) return result return K.mean(K.square(sigma(f, 273,601 )-sigma(g, 273,601), axis=-1) #22
現在のlosses.pyの中身
python
"""Built-in loss functions. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import six from . import backend as K from .utils.generic_utils import deserialize_keras_object from .utils.generic_utils import serialize_keras_object def originalloss(y_true,y_pred): def f(n): return y_pred[:,0]+y_pred[:,1]/n def g(n): return y_true[:,0]+y_true[:,1]/n def sigma(func, frm, to): result = 0; #答えの受け皿 for i in range(frm, to): result += func(i) return result return K.mean(K.square(sigma(f, 273,601 )-sigma(g, 273,601), axis=-1) #22 def mean_squared_error(y_true, y_pred): return K.mean(K.square(y_pred - y_true), axis=-1) def mean_absolute_error(y_true, y_pred): return K.mean(K.abs(y_pred - y_true), axis=-1) def mean_absolute_percentage_error(y_true, y_pred): diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true), K.epsilon(), None)) return 100. * K.mean(diff, axis=-1) def mean_squared_logarithmic_error(y_true, y_pred): first_log = K.log(K.clip(y_pred, K.epsilon(), None) + 1.) second_log = K.log(K.clip(y_true, K.epsilon(), None) + 1.) return K.mean(K.square(first_log - second_log), axis=-1) def squared_hinge(y_true, y_pred): return K.mean(K.square(K.maximum(1. - y_true * y_pred, 0.)), axis=-1) def hinge(y_true, y_pred): return K.mean(K.maximum(1. - y_true * y_pred, 0.), axis=-1) def categorical_hinge(y_true, y_pred): pos = K.sum(y_true * y_pred, axis=-1) neg = K.max((1. - y_true) * y_pred, axis=-1) return K.maximum(0., neg - pos + 1.) def logcosh(y_true, y_pred): """Logarithm of the hyperbolic cosine of the prediction error. `log(cosh(x))` is approximately equal to `(x ** 2) / 2` for small `x` and to `abs(x) - log(2)` for large `x`. This means that 'logcosh' works mostly like the mean squared error, but will not be so strongly affected by the occasional wildly incorrect prediction. # Arguments y_true: tensor of true targets. y_pred: tensor of predicted targets. # Returns Tensor with one scalar loss entry per sample. """ def _logcosh(x): return x + K.softplus(-2. * x) - K.log(2.) return K.mean(_logcosh(y_pred - y_true), axis=-1) def categorical_crossentropy(y_true, y_pred): return K.categorical_crossentropy(y_true, y_pred) def sparse_categorical_crossentropy(y_true, y_pred): return K.sparse_categorical_crossentropy(y_true, y_pred) def binary_crossentropy(y_true, y_pred): return K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1) def kullback_leibler_divergence(y_true, y_pred): y_true = K.clip(y_true, K.epsilon(), 1) y_pred = K.clip(y_pred, K.epsilon(), 1) return K.sum(y_true * K.log(y_true / y_pred), axis=-1) def poisson(y_true, y_pred): return K.mean(y_pred - y_true * K.log(y_pred + K.epsilon()), axis=-1) def cosine_proximity(y_true, y_pred): y_true = K.l2_normalize(y_true, axis=-1) y_pred = K.l2_normalize(y_pred, axis=-1) return -K.sum(y_true * y_pred, axis=-1) # Aliases. oriloss = originalloss mse = MSE = mean_squared_error mae = MAE = mean_absolute_error mape = MAPE = mean_absolute_percentage_error msle = MSLE = mean_squared_logarithmic_error kld = KLD = kullback_leibler_divergence cosine = cosine_proximity def serialize(loss): return serialize_keras_object(loss) def deserialize(name, custom_objects=None): return deserialize_keras_object(name, module_objects=globals(), custom_objects=custom_objects, printable_module_name='loss function') def get(identifier): """Get the `identifier` loss function. # Arguments identifier: None or str, name of the function. # Returns The loss function or None if `identifier` is None. # Raises ValueError if unknown identifier. """ if identifier is None: return None if isinstance(identifier, six.string_types): identifier = str(identifier) return deserialize(identifier) if isinstance(identifier, dict): return deserialize(identifier) elif callable(identifier): return identifier else: raise ValueError('Could not interpret ' 'loss function identifier:', identifier)
試したこと
mse = MSE = mean_squared_errorみたいな名前の定義が必要なのかと思いoriloss = originallossとも書いてloss=orilossともしたのですが、うまくいきませんでした。
まだ回答がついていません
会員登録して回答してみよう