AdamOptimizer返回无效的数据类型错误

时间:2018-11-07 12:32:02

标签: python-3.x tensorflow deep-learning conv-neural-network

在过去的几天里,我一直在努力解决此问题,我们将不胜感激。

我有一个网络,该网络在图像中输出数字位置x,y coordinates,因此输出int32数据类型。输出的大小为(2,2)。地面真理的位置也为int32

然后我运行以下两行代码:

cost = tf.reduce_mean(tf.square(y_true - y_pred_cls))
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)

并收到以下错误:

ValueError: Invalid type tf.int32 for Mean_7:0, expected: [tf.float32, tf.float64, tf.float16, tf.bfloat16].

我知道平均值应该是一个浮动值,但是如果将预测值转换为浮动值,就会开始出现“未提供渐变”错误。

我还尝试使用Tensorflow的softmax_cross_entropy_with_logits_v2将我的问题转变为分类问题,但存在相同的错误。我非常感谢您对解决方案的任何帮助。

代码

输入和标签代码

x_p = tf.placeholder(tf.float32, shape=[None, img_size_flat*num_channels], name='x_pred')
x_pred_image = tf.reshape(x_p, [-1, 10, 10, num_channels])
k = tf.constant(npatches)
y_true = tf.placeholder(tf.int32, shape=[None, 2, 2], name='y_true')
y_true_cls = tf.placeholder(tf.int32, shape=[None, 2, 2], name='y_true_cls')

卷积层

def new_weights(shape, layer_name):
    initializer = tf.contrib.layers.xavier_initializer()
    return tf.Variable(initializer(shape), name=layer_name+'_W')

def new_bias(length, layer_name):
    return tf.Variable(tf.constant(0.05, shape=[length]), name=layer_name+'_b')

def new_conv_layer(input,
                   num_input_channels,
                   filter_size,
                   num_filters,
                   name_scope,
                   layer_name='',
                   use_pooling=True):

    with tf.name_scope(name_scope):
        shape = [filter_size, filter_size, num_input_channels, num_filters]
        weights = new_weights(shape, layer_name)
        biases = new_bias(num_filters, layer_name)

        layer = tf.add(tf.nn.conv2d(input=input, filter=weights, strides=[1,1,1,1], padding='SAME'), biases, name=layer_name)

        if use_pooling:
            layer = tf.nn.max_pool(value=layer,
                                   ksize=[1, 3, 3, 1],
                                   strides=[1, 2, 2, 1],
                                   padding='SAME', name=layer_name+'_max')

        layer = tf.nn.relu(layer, name=layer_name+'_activation')

    return layer, weights

完全连接的层

def new_fc_layer(input,
                num_inputs,
                num_outputs,
                name_scope,
                layer_name='',
                use_relu=True):

    with tf.name_scope(name_scope):
        weights = new_weights([num_inputs, num_outputs], layer_name)
        biases = new_bias(num_outputs, layer_name)

        layer = tf.add(tf.matmul(input, weights),biases,name=layer_name)
    #     layer = tf.matmul(input, weights) + biases

        if use_relu:
            layer = tf.nn.relu(layer, layer_name+'_activation')

    return layer

自定义图层代码

with tf.name_scope(name_scope):
    resh_inp = tf.reshape(input, [-1, 10, 10])
    input_shape = tf.shape(resh_inp)
    rows, cols = input_shape[1], input_shape[2]
    d_rows, d_cols = 2, 2
    subm_rows, subm_cols = rows - d_rows + 1, cols - d_cols + 1
    ii, jj = tf.meshgrid(tf.range(subm_rows), tf.range(subm_cols), indexing='ij')
    d_ii, d_jj = tf.meshgrid(tf.range(d_rows), tf.range(d_cols), indexing='ij')

    subm_ii = ii[:, :, tf.newaxis, tf.newaxis] + d_ii
    subm_jj = jj[:, :, tf.newaxis, tf.newaxis] + d_jj
    subm = tf.gather_nd(resh_inp[0, :, :], tf.stack([subm_ii, subm_jj], axis=-1), name=layer_name + "_gather")
    subm_sum = tf.reduce_sum(subm, axis=(2, 3), name=layer_name + "_subm_sum")
    _, top_idx = tf.nn.top_k(tf.reshape(subm_sum, [-1]), tf.minimum(k, tf.size(subm_sum)), name=layer_name + "_top_idx")
    top_row = top_idx // subm_cols
    top_col = top_idx % subm_cols
    result = tf.stack([top_row, top_col], axis=-1, name=layer_name + "_result")
    result_shape = tf.shape(result)
    result = tf.reshape(result, [-1, result_shape[0], result_shape[1]], name=layer_name + "_shaped_result")

上面代码的结果在下面的代码中以y_pred_cls的形式传递

cost = tf.reduce_mean(tf.square(y_true - y_pred_cls))
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)

0 个答案:

没有答案