简体   繁体   English

同时获得“'numpy.ndarray'对象不可调用”和“'Tensor'对象不可调用”

[英]getting both “'numpy.ndarray' object is not callable” and “'Tensor' object is not callable”

I'm working on building a binary classifier: I'm inexperienced in ML so, using code adapted from the Iris classification tutorial on TensorFlow.org I'm getting 85% accuracy on the test set. 我正在构建二进制分类器:我对ML没有经验,因此,使用从TensorFlow.org上的Iris分类教程改编的代码,我在测试集上获得了85%的准确性。 However, this evaluation is run using a threshold value of 0.5: I'd like to be able to try different threshold values just to see if I can get better accuracy. 但是,此评估是使用0.5的阈值进行的:我希望能够尝试不同的阈值,只是为了看看我是否可以获得更好的准确性。 So I dug into the tensorflow website and found the following command: 所以我进入tensorflow网站并找到以下命令:

tf.metrics.precision_at_thresholds(
    labels,
    predictions,
    thresholds,
    weights=None,
    metrics_collections=None,
    updates_collections=None,
    name=None
)

which looks just like what I need because it would allow me to evaluate the accuracy using whatever custom threshold values I want. 看起来就像我需要的,因为它可以让我使用所需的任何自定义阈值来评估准确性。 Thus, I added this bit to the code for my model and the end result is looking like this: 因此,我将此代码添加到了模型代码中,最终结果看起来像这样:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
train_file = "/home/javier/train.csv"
test_file = "/home/javier/test.csv"
def main():
    # Load datasets.
    training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
        filename=train_file,
        target_dtype=np.int,
        features_dtype=np.float32)
    test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
        filename=test_file,
        target_dtype=np.int,
        features_dtype=np.float32)
    # Specify that all features have real-value data
    feature_columns = [tf.contrib.layers.real_valued_column("", dimension=15)]
    # Build 3 layer DNN with 10, 20, 10 units respectively.
    classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
            hidden_units=[15,20,15],
            optimizer=tf.train.ProximalAdagradOptimizer(learning_rate=0.05,l2_regularization_strength=0.2),
            n_classes=2,
            model_dir="/home/javier/tf_tinkering")
    # Define the training inputs
    def get_train_inputs():
        x = tf.constant(training_set.data)
        y = tf.constant(training_set.target)
        return x, y
    # Fit model.
    classifier.fit(input_fn=get_train_inputs, steps=50)
    # Define the test inputs
    def get_test_inputs():
        x = tf.constant(test_set.data)
        y = tf.constant(test_set.target)
        return x, y
    # Evaluate accuracy.
    tf.metrics.precision_at_thresholds(
        tf.constant(test_set.target),
        classifier.predict(input_fn=tf.constant(test_set.data)),
        thresholds=[0.5,0.4,0.6],
    )

if __name__ == "__main__":
    main()

The problem is that tf.metrics isn't interpreting the "predictions" bit. 问题在于tf.metrics无法解释“预测”位。 I've tried different ways of calling "predictions" and they all return an error. 我尝试了不同的调用“预测”的方法,它们都返回错误。 Using 运用

tf.metrics.precision_at_thresholds(
        tf.constant(test_set.target),
        classifier.predict_classes(input_fn=get_test_inputs),
        thresholds=[0.5,0.4,0.6],
    )

gives me 给我

TypeError: Expected binary or unicode string, got <generator object <genexpr> at 0x7ff38d2c5af0>

Using 运用

tf.metrics.precision_at_thresholds(
        tf.constant(test_set.target),
        classifier.predict_classes(input_fn=tf.constant(test_set.data)),
        thresholds=[0.5,0.4,0.6],
    )

results in 结果是

TypeError: 'Tensor' object is not callable

and using 和使用

tf.metrics.precision_at_thresholds(
        tf.constant(test_set.target),
        classifier.predict_classes(input_fn=test_set.data),
        thresholds=[0.5,0.4,0.6],
    )

outputs 输出

TypeError: 'numpy.ndarray' object is not callable

I even tried defining a new matrix "feature_columns_matrix" and pasted all the values into it from the csv file and ran "classifier.predict_classes(input_fn=feature_columns_matrix)" it also didn't work. 我什至尝试定义一个新的矩阵“ feature_columns_matrix”,然后将其从csv文件中粘贴到所有值中,然后运行“ classifier.predict_classes(input_fn = feature_columns_matrix)”,该矩阵也无法正常工作。 How do I pass the values of the output layer of my network when run on the test set onto the tf.metrics subroutine? 在测试集上运行到tf.metrics子例程时,如何传递网络输出层的值?

I've already read about 10 other similar questions on this website and none have helped me (just so you know I'm not asking a redundant question). 我已经在该网站上阅读了大约10个其他类似问题,但没有一个对我有帮助(只是您知道我不是在问一个多余的问题)。 Any help will be greatly appreciated! 任何帮助将不胜感激! Thanks 谢谢

UPDATE: I found out that running 更新:我发现正在运行

print(list(classifier.predict(input_fn=get_test_inputs)))

correctly returns the predicted classes for each sample in the test file. 正确返回测试文件中每个样本的预测类。 However, that's not quite what I need for tf.metrics to evaluate accuracy because the above command returns classes precisely based on the 0.5 threshold! 但是,这不是我为tf.metrics评估准确性所需要的,因为上述命令正是基于0.5阈值返回类! It doesn't give the actual output of the final layer of the net. 它没有给出网络最后一层的实际输出。 I get this when I ran the command above: 当我运行上面的命令时,我得到了:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

But what I really need is the actual float32 values it produces when the net runs on the test set. 但是我真正需要的是当网络在测试集上运行时产生的实际float32值。 That way I can feed that into the tf.metrics and test different threshold values. 这样,我可以将其输入tf.metrics并测试不同的阈值。 Does anybody know how to do that? 有人知道该怎么做吗?

TypeError: 'numpy.ndarray' object is not callable

This means you have a numpy array at this point, and you are trying to use as though it were a function. 这意味着您此时有一个numpy数组,并且您试图将其当作函数使用。 That is, you are 'calling' it with arr(...) syntax. 也就是说,您正在使用arr(...)语法“调用”它。 Either you should be indexing it arr[...] , or this object shouldn't be an array in the first place. 您要么应该为arr[...]为其建立索引,要么该对象首先不应该是数组。

Same for the tensor object. 与张量对象相同。

TypeError: Expected binary or unicode string, got <generator object <genexpr> at 0x7ff38d2c5af0>

means that the function expected a string as a parameter, but you are giving it something else (the concept of a generator is probably too advanced for you.) 表示该函数希望将字符串作为参数,但是您给了它其他东西( generator的概念可能对您而言太高级了。)

Ideally when programming in Python you need to understand what each variable is referencing, specifically what kind of object. 理想情况下,使用Python进行编程时,您需要了解每个变量所引用的对象,尤其是哪种对象。 Is is a function, a string, a number, an array, and tensorflow object. 是一个函数,一个字符串,一个数字,一个数组和tensorflow对象。 When you have bugs like this you need to add diagnostic prints that check what the objects. 当您遇到此类错误时,您需要添加诊断打印来检查对象。 Don't just assume; 不要只是假设; test. 测试。

Apparently you are trying different things in this expression: 显然,您在此表达式中尝试不同的事情:

classifier.predict_classes(input_fn=get_test_inputs)
classifier.predict_classes(input_fn=tf.constant(test_set.data))
classifier.predict_classes(input_fn=test_set.data)

What does this function accept? 此功能接受什么? I'm guessing test_set.dat is a numpy array. 我猜test_set.dat是一个numpy数组。 Wrapping that in tf.constant() turns it into a tensor object. 将其包装在tf.constant()其转变为张量对象。 While get_test_inputs is a generator/function. get_test_inputs是一个生成器/函数。

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

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