I am having an error - 'Tensor' object has no attribute 'log' that I code in Keras to build a network while apply custom loss function to Keras. I think somehow I need to get rid of np.log but not sure how.
import Numpy
import numpy as np
Custom Function
def rmsle(y_pred,y_test):
return np.sqrt(np.mean((np.log(1+y_pred) - np.log(1+y_test))**2))
My network
def base_model():
model = Sequential()
model.add(Dense(50, input_dim=X_train.shape[1], init='normal', activation='sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(1, init='normal'))
sgd = SGD(lr=0.01, momentum=0.8, decay=0.1, nesterov=False)
model.compile(loss=rmsle, optimizer = sgd)# )'adam') #
return model
keras = KerasRegressor(build_fn=base_model, nb_epoch=80, batch_size=1,verbose=1)
keras.fit(X_train ,y_train)
When i check the error msg in detail, it shows that
424 """
425 # score_array has ndim >= 2
--> 426 score_array = fn(y_true, y_pred)
427 if mask is not None:
428 # Cast the mask to floatX to avoid float64 upcasting in theano
2 #return np.sqrt(np.mean(np.square( np.log( (np.exp(a)) + 1 ) - np.log((np.exp(b))+1) )))
----> 4 return np.sqrt(np.mean((np.log(1+y_pred) - np.log(1+y_test))**2))
2 #return np.sqrt(np.mean(np.square( np.log( (np.exp(a)) + 1 ) - np.log((np.exp(b))+1) )))
numpy, ideally show us a Minimal, Complete, and Verifiable example of your problem. My first suspicion would be that you re-assignnpto something other thannumpy, so that instead of callingnumpy.log, you are trying to call whatever Keras object you assigned tonp.print(type(np), np.__file__)into your loss function to check thatnpreally is thenumpymodule.