首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow classifier.evaluate无限运行?

Tensorflow classifier.evaluate无限运行?
EN

Stack Overflow用户
提问于 2019-08-27 21:28:47
回答 1查看 79关注 0票数 0

我已经开始尝试一些Tensorflow API。我正在使用虹膜数据集来实验Tensorflow Estimator。

我的问题是,当下面的代码执行时,我使用:

代码语言:javascript
复制
   # Evaluate the model.
   eval_result = classifier.evaluate(

我的电脑似乎没有尽头。我已经等了一个半小时了,我一直在等待我的笔记本完成这一步,但看不到尽头。笔记本的最后输出是:

信息:tensorflow:运行local_init_op。

信息:tensorflow:完成运行local_init_op。

问题陈述:我如何调整我的代码,以使这个评估更有效率,我显然让它做了比我预期的更多的工作。

到目前为止,我已经尝试调整批次大小和层中的数目或神经元,但没有运气。

代码语言:javascript
复制
#First we want to import what we need. Typically this will be some combination of:
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder 

%matplotlib inline

#Extract the data from the iris dataset.
df = pd.read_csv('IRIS.csv')
le = LabelEncoder()
df['species'] = le.fit_transform(df['species'])

#Extract both into features and labels.
#features should be a dictionary.
#label can just be an array
def extract_features_and_labels(dataframe):

    #features and label for training
    x = dataframe.copy()
    y = dataframe.pop('species')
    return dict(x), y

#break the data up into train and test.
#split the overall df into training and testing data
train, test = train_test_split(df, test_size=0.2)

train_x, train_y = extract_features_and_labels(train)
test_x, test_y = extract_features_and_labels(test)

print(len(train_x), 'training examples')
print(len(train_y), 'testing examples')

my_feature_columns = []
for key in train_x.keys():
    my_feature_columns.append(tf.feature_column.numeric_column(key=key))

def train_input_fn(features, labels, batch_size):
    """An input function for training"""
    # Convert the inputs to a Dataset.
    dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))

    # Shuffle, repeat, and batch the examples.
    return dataset.shuffle(1000).repeat().batch(batch_size)

#Build the classifier!!!!
classifier = tf.estimator.DNNClassifier(
    feature_columns=my_feature_columns,
    # Two hidden layers of 10 nodes each.
    hidden_units=[4, 4],
    # The model must choose between 3 classes.
    n_classes=3)

# Train the Model.
classifier.train(
    input_fn=lambda:train_input_fn(train_x, train_y, 10), steps=1000)

# Evaluate the model.
eval_result = classifier.evaluate(
    input_fn=lambda:train_input_fn(test_x, test_y, 100))

print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-05 16:58:28

不得不切换很多东西,但最终得到了IRIS数据集的估计器。以下是为将来可能发现有用的人编写的代码。干杯。

代码语言:javascript
复制
#First we want to import what we need. Typically this will be some combination of:
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

from sklearn.preprocessing import LabelEncoder  
from sklearn import preprocessing

%matplotlib inline

#Extract the data from the iris dataset.
df = pd.read_csv('IRIS.csv')

#Grab only our categorial data
#categories = df.select_dtypes(include=[object])
le = LabelEncoder()
df['species'] = le.fit_transform(df['species'])
# use df.apply() to apply le.fit_transform to all columns
#X_2 = X.apply(le.fit_transform)

#Reshape as the one hot encoder doesnt like one row/column
#X_2 = X.reshape(-1, 1)

#features is everything but our label so makes sense to simply....
features = df.drop('species', axis=1)
print(features.head())
target = df['species']
print(target.head())

#trains and test
X_train, X_test, y_train, y_test = train_test_split(
     features, target, test_size=0.33, random_state=42)

#Introduce Tensorflow feature column (numeric column)
numeric_column = ['sepal_length','sepal_width','petal_length','petal_width']

numeric_features = [tf.feature_column.numeric_column(key = column)
                   for column in numeric_column]
print(numeric_features[0])

#Build the input function for training
training_input_fn = tf.estimator.inputs.pandas_input_fn(x = X_train,
                                                       y=y_train,
                                                       batch_size=10,
                                                       shuffle=True,
                                                       num_epochs=None)

#Build the input function for testing input
eval_input_fn = tf.estimator.inputs.pandas_input_fn(x=X_test,
                                                   y=y_test,
                                                   batch_size=10,
                                                   shuffle=False,
                                                   num_epochs=1)

#Instansiate the model
dnn_classifier = tf.estimator.DNNClassifier(feature_columns=numeric_features,
                                              hidden_units=[3,3],
                                              optimizer=tf.train.AdamOptimizer(1e-4),
                                              n_classes=3,
                                              dropout=0.1,
                                              model_dir = "dnn_classifier")

dnn_classifier.train(input_fn = training_input_fn,steps=2000)

#Evaluate the trained model
dnn_classifier.evaluate(input_fn = eval_input_fn)
# print("Loss is " + str(loss))

pred = list(dnn_classifier.predict(input_fn = eval_input_fn))

for e in pred:
    print(e)
    print("\n")
#pred = [p['species'][0] for p in pred]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57682485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档