1
from sklearn.model_selection import train_test_split 
from sklearn.metrics import mean_squared_error 
import statistics 
import keras 
import numpy as np 
import pandas as pd 
from keras.models import Sequential 
from keras.layers import Dense 
import tensorflow as tf 
import textdistance 
import sys 
print(tf.executing_eagerly())





def custom_error_finder(y_actual,y_pred):
        print(tf.executing_eagerly())
        count = 0
        ya = ((y_actual[0].numpy()).decode())
        yp = ((y_pred[0].numpy()).decode())
        for i,j in ya,yp:
            if i!=j:
                count = count+1
        mse = pow(count,2)/len(ya)
        return mse


model = Sequential()
scraper_data = pd.read_csv("C:/Users/Desktop/Projects/Chatbot/Combined_new.csv")

scraper_data_columns = scraper_data.columns

predictors = scraper_data["Given"] 
target = scraper_data['Target'] 

X_train, X_test, y_train, y_test = train_test_split(predictors, target, test_size=0.3, random_state=42)
n_cols = predictors.shape[0]

model.add(Dense(1, activation = 'relu', input_shape = (n_cols,)))
model.add(Dense(10, activation = 'relu'))
model.add(Dense(10, activation = 'relu'))
model.add(Dense(1, activation = 'relu'))

model.compile(optimizer = 'adam',run_eagerly=True, loss = custom_error_finder)
model.run_eagerly = True
model.fit(X_train, y_train,epochs=200)
predictions = model.predict(X_test)

I am extremely new to designing deep learning models and this is literally my first model, ever.

I have defined a custom error metric and for some reason i get this error:

AttributeError: 'Tensor' object has no attribute 'numpy'

I don't know if this helps but the outer "print(tf.executing_eagerly())" print "True" while the inner one prints "False".

"y_actual" are the true values from the dataset and "y_pred" are the predicted values by the model.

I would honestly appreciate any help I can get on this since I've been working on this model for weeks now.

Thanks in advance.

(The "Given" column has strings in it and the "Target" column has strings I'm trying to extract from the "Given" column)

BSP
  • 342
  • 2
  • 3
  • 14
Spartacus98
  • 82
  • 1
  • 9
  • Does this answer your question? [AttributeError: 'Tensor' object has no attribute 'numpy'](https://stackoverflow.com/questions/52357542/attributeerror-tensor-object-has-no-attribute-numpy) – Jodh Singh May 12 '20 at 16:12

1 Answers1

0

For enabling Eager execution you have to use

import TensorFlow as tf
tf.enable_eager_execution()
tf.executing_eagerly()

for more use enter link description here

BSP
  • 342
  • 2
  • 3
  • 14
  • 1
    I tried doing that but it says "AttributeError: module 'tensorflow' has no attribute 'enable_eager_execution'". Moreover, doesn't tensorflow 2.0+ have eager execution by default? – Spartacus98 May 12 '20 at 11:21
  • before using `.numpy()` you have to convert y_actual and y_pred to tensors using `tf.convert_to_tensor(y_actual )` – BSP May 12 '20 at 11:42