简体   繁体   English

如何在我的自定义损失 function 中将张量保存到 Numpy 数组?

[英]How to save tensor to Numpy array in my customized loss function?

I want to check my middle result when training the model.我想在训练 model 时检查我的中间结果。 So, I need to save tensor out in my customized loss.所以,我需要在我的自定义损失中保存张量。

here is my code:这是我的代码:

from util import *
from tensorflow import keras
from tensorflow.keras import layers
import tensorflow as tf
from gen_model import read_cache_data
from numpy import random
from ml_util import *
from catboost import CatBoostRegressor
import warnings
warnings.filterwarnings('ignore')

class myloss(keras.losses.Loss):
  def __init__(self, coef, name='myloss'):
    super().__init__(name=name)
    self.coef = coef

  def call(self, y, y_pred):
    # I want to save y_pred here, the following is the method i tried, none of them works!!!!!!!!!!
    #a = (tf.print(y_pred))
    #b = (tf.print(y))
    print(type(y_pred))
    #sess = tf.Session();
    sess = tf.compat.v1.Session()
    with sess.as_default(): print(y_pred.eval())
    #print(y_pred.eval())
    #print(y_pred.numpy())
    return tf.math.reduce_mean(tf.square(y - y_pred), axis=1)

def train_mlp(train_df, valid_df, test_df, fv_cols, res_col):
  callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=10)
  model = keras.Sequential([layers.Dense(50, input_shape=(len(fv_cols), ), activation='relu'), layers.Dense(30, activation='relu'), layers.Dense(1)])
  model.compile(optimizer=keras.optimizers.SGD(0.1), loss = myloss(0.1))
  model.summary()
  #sess.run(tf.compat.v1.global_variables_initializer())
  model.fit(train_df[fv_cols], np.reshape(train_df[res_col].tolist(), (-1, 1)), callbacks=[callback], validation_data=(valid_df[fv_cols], valid_df[res_col]), epochs=100, batch_size=65536)
  train_pred = (model.predict(train_df[fv_cols])).flatten()
  test_pred = (model.predict(test_df[fv_cols])).flatten()
  d = pd.DataFrame([train_pred, train_df[res_col], test_pred, test_df[res_col]]).T
  d.columns = ['train_pred', 'train_y', 'test_pred', 'test_y']
  print(d)
  print('MLP is R2 =', r2(y_pred = train_pred, y = train_df[res_col]))
  print('MLP os R2 =', r2(y_pred = test_pred, y = test_df[res_col]))


if __name__ == '__main__':
  df = read_cache_data('cache')
  df = df.replace(-np.inf, np.nan).replace(np.inf, np.nan).dropna()
  fv_cols = df.columns[21:-3]
  res_col = 'res_10'
  train, test_df = df.iloc[:int(0.5*len(df))], df.iloc[int(0.5*len(df)):]
  train = train.sample(frac=1, random_state=1).reset_index(drop=True)
  train_df, valid_df = train.iloc[:int(0.7*len(train))], train.iloc[int(0.7*len(train)):]
  train_mlp(train_df, valid_df, test_df, fv_cols, res_col)

I tried some methods, include, eval() , session.run() but none of them worked,我尝试了一些方法,包括eval()session.run()但它们都不起作用,
for eval: the error is:对于评估:错误是:

ValueError: Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an ex

for session, the error is:对于 session,错误是:

InvalidArgumentError: You must feed a value for placeholder tensor 'sequential/dense/MatMul/ReadVariableOp/resource' with dtype reso
         [[node sequential/dense/MatMul/ReadVariableOp/resource (defined at lstm.py:58) ]]

Can anyone help with this?有人能帮忙吗?

Something like this像这样的东西

# custom loss function 
class myloss(tf.keras.losses.Loss):
  def __init__(self, coef=None, name='myloss'):
    super().__init__(name=name)
    self.coef = coef
  def call(self, y, y_pred):
    return tf.math.reduce_mean(tf.square(y - y_pred), axis=1)

# some dummines 
import numpy as np 
y_true = np.array([[0., 1.], [0., 0.]])
y_pred = np.array([[1., 1.], [1., 0.]])

# calling function
m = myloss()
m(y_true, y_pred).numpy()
0.5

# saving loss value into numpy array and reloading 
np.save('./loss', m(y_true, y_pred).numpy())
rloss = np.load('loss.npy')
rloss
0.5

I just notice your comment in the call method, where you mentioned saving y_pred .我只是注意到你在call方法中的评论,你提到保存y_pred Not fully sure if I'm properly following you but here is some catch (and let me know on this):不完全确定我是否正确地关注你,但这里有一些问题(让我知道这一点):

def call(self, y, y_pred):
      np.save('./loss', y_pred)
      return tf.math.reduce_mean(tf.square(y - y_pred), axis=1)

...
rloss = np.load('loss.npy')
rloss
array([[1., 1.],
       [1., 0.]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM