【问题标题】:Model not executing eagerly in tensorflow 2.0 with keras使用 keras 在 tensorflow 2.0 中模型未急切执行
【发布时间】:2020-08-28 02:19:31
【问题描述】:
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)

我对设计深度学习模型非常陌生,这实际上是我的第一个模型。

我已经定义了一个自定义错误指标,但由于某种原因我得到了这个错误:

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

我不知道这是否有帮助,但外部“print(tf.executing_eagerly())”打印“True”,而内部打印“False”。

“y_actual”是数据集中的真实值,“y_pred”是模型的预测值。

老实说,由于我已经在这个模型上工作了几个星期,因此我非常感谢能在这方面得到任何帮助。

提前致谢。

(“Given”列中包含字符串,“Target”列包含我试图从“Given”列中提取的字符串)

【问题讨论】:

标签: numpy tensorflow deep-learning tensor eager-execution


【解决方案1】:

要启用 Eager 执行,您必须使用

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

更多使用enter link description here

【讨论】:

  • 我试过这样做,但它说“AttributeError:模块'tensorflow'没有属性'enable_eager_execution'”。而且,tensorflow 2.0+默认没有eager execution吗?
  • 在使用 .numpy() 之前,您必须使用 tf.convert_to_tensor(y_actual ) 将 y_actual 和 y_pred 转换为张量
猜你喜欢
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 2019-06-23
  • 2019-08-29
  • 2019-08-28
相关资源
最近更新 更多