gpt4 book ai didi

python - tensorflow 错误 : unsupported callable - (from ex ? ??)

转载 作者:太空宇宙 更新时间:2023-11-03 21:04:02 44 4
gpt4 key购买 nike

我正在尝试将教程“Build a Convolutional Neural Network using Estimators”中的 CNN 调整为我的数据集,但不知道如何修复此错误

...输入文件应该没问题,因为它们已经过测试并且没问题,因为我目前正在另一个 CNN 上运行它们,但有很大不同(它工作正常,但我愿意更改它,添加一些额外的功能,如“dropout”)

事实上,这个错误(我使用Spyder作为IDE)是毫无意义的。我已经做了一些尝试来查看错误所在,但我有点越来越困惑,所以让我们试着问问你们

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.INFO)

#----- global variables Start ------
nb_of_neurons=1024
model_learning_rate=0.001

#----- global variables End ------

def run_cnn(mymode, last_date, names, mydata, mylabels, run_id):
def cnn_model_fn(cnndata, mylabels, mode):
input_layer = tf.reshape(cnndata, [-1, 4, 5, 1])
conv = tf.layers.conv2d(
inputs=input_layer,
filters=16,
kernel_size=[2, 3],
padding="same",
activation=tf.nn.relu)
print(conv.shape.dims)
pool = tf.layers.max_pooling2d(inputs=conv, pool_size=[2, 2], strides=2)
pool_dims=pool.shape.as_list()[1]*pool.shape.as_list()[2]*pool.shape.as_list()[3]
pool_flat = tf.reshape(pool, [-1, pool_dims])
dense = tf.layers.dense(inputs=pool_flat, units=nb_of_neurons, activation=tf.nn.relu)
dropout = tf.layers.dropout(
inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)
logits = tf.layers.dense(inputs=dropout, units=2)
predictions = {
"classes": tf.argmax(input=logits, axis=1),
"probabilities": tf.nn.softmax(logits, name="softmax_tensor")
}
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

loss = tf.losses.sparse_softmax_cross_entropy(labels=mylabels, logits=logits)
print(loss)
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=model_learning_rate)
train_op = optimizer.minimize(
loss=loss,
global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
eval_metric_ops = {
"accuracy": tf.metrics.accuracy(
labels=mylabels, predictions=predictions["classes"])
}
return tf.estimator.EstimatorSpec(
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)


if mymode == 'TRAIN':
mode= tf.estimator.ModeKeys.TRAIN
cnn_classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn(mydata, mylabels, mode), model_dir="/sess")

tensors_to_log = {"probabilities": "softmax_tensor"}

logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=50)

train_input_fn = tf.estimator.inputs.numpy_input_fn(
x=mydata,
y=mylabels,
batch_size=100,
num_epochs=None,
shuffle=True)

cnn_classifier.train(
input_fn=train_input_fn,
steps=1,
hooks=[logging_hook])
cnn_classifier.train(input_fn=train_input_fn, steps=1000)

elif mymode == 'PREDICT':
mode= tf.estimator.ModeKeys.PREDICT
cnn_classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn(mydata, mylabels, mode), model_dir="/sess")

tensors_to_log = {"probabilities": "softmax_tensor"}

logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=50)

eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x=mydata,
y=mylabels,
num_epochs=1,
shuffle=False)
eval_results = cnn_classifier.evaluate(input_fn=eval_input_fn)

else:
print('**** ->*** ???? ***')

这从另一个传递给所有输入数据的 python 脚本中作为模块调用,如下所示:

  1. mymode:在 ['PREDICT', 'TRAIN'] 中
  2. last_date:不相关
  3. 名称:不相关
  4. mydata:形状为 (3195,20) 的 np 数组,值在 [0., 1.](浮点型)中
  5. mylabels:形状为 (3195,) 的 np 数组,值在 [0, 1] (int) 中
  6. run_i:不相关

最后,错误出现在train_op之后(即在tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)中),如下:

...
File "C:\Users\Fulviooo\Anaconda3\lib\site-packages\tensorflow\python\util\function_utils.py", line 56, in fn_args
args = tf_inspect.getfullargspec(fn).args

File "C:\Users\Fulviooo\Anaconda3\lib\site-packages\tensorflow\python\util\tf_inspect.py", line 216, in getfullargspec
if d.decorator_argspec is not None), _getfullargspec(target))

File "C:\Users\Fulviooo\Anaconda3\lib\inspect.py", line 1095, in getfullargspec
raise TypeError('unsupported callable') from ex

TypeError: unsupported callable

希望有人能告诉我错误在哪里以及如何解决。此外,我很高兴收到任何其他改进建议。

谢谢

最佳答案

实际上,问题在于该估计器非常严格,并且需要具有预定义名称和格式的变量。即设置预期的名称:

train_data=mydata
train_labels=mylabels

和格式(字典):

x={"x": train_data}

然后运行

关于python - tensorflow 错误 : unsupported callable - (from ex ? ??),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55543880/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com