Tensorflow的完全连接功能要求缺少参数' num_outputs'即使它被指定

时间:2018-01-14 14:58:19

标签: python-3.x tensorflow

我正在尝试使用两个完全连接的隐藏层构建张量流图。在运行代码时,我收到一个错误:

" fully_connected()缺少1个必要的位置参数:' num_outputs'"

即使参数值指定如下。

tf.reset_default_graph()

n_input = 15
n_output = 1
n_hidden1 = 300
n_hidden2 = 300

X = tf.placeholder(tf.float32, shape=(None, n_input), name = 'X')
y = tf.placeholder(tf.float64, shape=(None,n_output), name = 'y')
is_training = tf.placeholder(tf.bool, shape = (), name='is_training')

bn_params = {
'is_training': True,
'decay': 0.99,
'updates_collections': None,
'scale': True
}

hidden1 = fully_connected(inputs=X, num_outputs=n_hidden1, scope = 
'hidden1', activation_fn= relu, normalizer_fn=batch_norm, normalizer_params=bn_params)
hidden2 = fully_connected(inputs=hidden1, num_outputs=n_hidden2, scope = 'hidden2', activation_fn= elu, normalizer_fn=batch_norm, normalizer_params=bn_params)
output = fully_connected(inputs=hidden2, num_outputs=n_output, scope = 'output', activation_fn= None, normalizer_fn=batch_norm, normalizer_params=bn_params)

cost = tf.sqrt(tf.metrics.mean_squared_error(labels=y, predictions= output, name = 'cost'))
training_op = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

请让我知道我在哪里弄错了。

错误输出如下所示:

TypeError                                 Traceback (most recent call last)
<ipython-input-35-cd34abf252e6> in <module>()
     17 }
     18 
---> 19 hidden1 = fully_connected(inputs=X, num_outputs = n_hidden1, 
scope = 'hidden1', activation_fn= relu,                           
normalizer_fn=batch_norm, normalizer_params=bn_params)
 20 hidden2 = fully_connected(inputs=hidden1, num_outputs = n_hidden2, 
scope = 'hidden2', activation_fn= elu,                           
normalizer_fn=batch_norm, normalizer_params=bn_params)
 21 output = fully_connected(inputs=hidden2, num_outputs=n_output, 
scope = 'output', activation_fn= None,                           
normalizer_fn=batch_norm, normalizer_params=bn_params)

~/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py in func_with_args(*args, **kwargs)
    179       current_args = current_scope[key_func].copy()
    180       current_args.update(kwargs)
--> 181     return func(*args, **current_args)
    182   _add_op(func)
    183   setattr(func_with_args, '_key_op', _key_op(func))

~/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/layers/python/layers/layers.py in fully_connected(inputs, num_outputs, activation_fn, normalizer_fn, normalizer_params, weights_initializer, weights_regularizer, biases_initializer, biases_regularizer, reuse, variables_collections, outputs_collections, trainable, scope)
   1651 
   1652     if activation_fn is not None:
-> 1653       outputs = activation_fn(outputs)
   1654 
   1655     return utils.collect_named_outputs(outputs_collections, sc.name, outputs)

~/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py in func_with_args(*args, **kwargs)
    179       current_args = current_scope[key_func].copy()
    180       current_args.update(kwargs)
--> 181     return func(*args, **current_args)
    182   _add_op(func)
    183   setattr(func_with_args, '_key_op', _key_op(func))

TypeError: fully_connected() missing 1 required positional argument: 'num_outputs'

0 个答案:

没有答案