Tensorflow 计算loss时的维度问题

发布于 2022-09-07 03:25:11 字数 6456 浏览 9 评论 0

在跑一个简单的CNN模型, 图片为64X64
第一层卷积为8X5X5,池化为5X5,步长为2;
第二层卷积为16X5X5,池化为5X5,步长为2;
第三层卷积为32X1X1,池化为全局池化 16X16
输出1X1X32的特征值,展平后输入全连接层
第四层全连接层 输出为2维
算出来最后输入全连接层的特征值应该有32个 输出2个值

batch_size为20
但在使用sparse_softmax_cross_entropy计算交叉熵损失的时候 一直报维度错误:
logits and labels must have the same first dimension, got logits shape [1280,2] and labels shape [20]

代码如下:

# -*- coding: utf-8 -*-

# Steganalysis with High-Level API 

# import dataset
import load_record

import tensorflow as tf
import numpy as np
import layer_module

flags = tf.app.flags

flags.DEFINE_integer('num_epochs', 10, 'Number of training epochs')
flags.DEFINE_integer('batch_size', 20, 'Batch size')
flags.DEFINE_float('learning_rate', 0.01, 'Learning rate')
flags.DEFINE_float('dropout_rate', 0.5, 'Dropout rate')

flags.DEFINE_string('train_dataset', './dataset/train512.tfrecords',
                    'Filename of training dataset')
flags.DEFINE_string('eval_dataset', './dataset/test512.tfrecords',
                    'Filename of evaluation dataset')
flags.DEFINE_string('test_dataset', './dataset/test512.tfrecords',
                    'Filename of testing dataset')
flags.DEFINE_string('model_dir', 'models/steganalysis_cnn_model',
                    'Filename of testing dataset')

FLAGS = flags.FLAGS

def stg_model_fn(features, labels, mode):
    # Input Layer
    x = tf.reshape(features, [-1, 64, 64, 1])
    # print(x)
    x = layer_module.conv_group(
        inputs = x,
        activation = "tanh",
        filters = 8,
        kernel_size = [5, 5],
        pool_size = 5,
        strides = 2,
        abs_layer = True,
        pool_padding = "same")
    print(x)

    x = layer_module.conv_group(
        inputs = x,
        filters = 16,
        activation = "tanh",
        kernel_size = [5, 5],
        pool_size = 5,
        strides = 2,
        abs_layer = False,
        pool_padding = "same")
    print(x)

    x = layer_module.conv_group(
        inputs = x,
        filters = 32,
        activation = "relu",
        kernel_size = [1, 1],
        pool_size = 16,
        strides = 1,
        abs_layer = False,
        pool_padding = "valid")
    print(x)


    x = tf.reshape(x, [-1, 32])
    x = tf.layers.dense(inputs = x, units = 2)
    
    # x = tf.contrib.layers.flatten(inputs = x)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print(tf.nn.softmax(x, name="softmax_tensor").eval(), labels.shape)
    predictions = {
        # Generate predictions (for PREDICT and EVAL mode)
        "classes": tf.argmax(input=x, axis=1),
        # Add `softmax_tensor` to the graph. It is used for PREDICT and by the
        # `logging_hook`.
        "probabilities": tf.nn.softmax(x, name="softmax_tensor")
        }
    
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
        # Calculate Loss (for both TRAIN and EVAL modes)
    onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=2)
    
    loss = tf.losses.sparse_softmax_cross_entropy(labels = labels, logits = x)
        # Configure the Training Op (for TRAIN mode)
    if mode == tf.estimator.ModeKeys.TRAIN:
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=FLAGS.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)
        # Add evaluation metrics (for EVAL mode)
    eval_metric_ops = {
            "accuracy": tf.metrics.accuracy(
                labels=labels, predictions=predictions["classes"])}
    return tf.estimator.EstimatorSpec(
            mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)

def parser(record):
    keys_to_features = {
        'img_raw': tf.FixedLenFeature((), tf.string),
        'label': tf.FixedLenFeature((), tf.int64)
    }
    parsed = tf.parse_single_example(record, keys_to_features)
    image = tf.decode_raw(parsed['img_raw'], tf.uint8)
    image = tf.cast(image, tf.float32)
    label = tf.cast(parsed['label'], tf.int32)
    return image, label


def save_hp_to_json():
    '''Save hyperparameters to a json file'''
    filename = os.path.join(FLAGS.model_dir, 'hparams.json')
    hparams = FLAGS.flag_values_dict()
    with open(filename, 'w') as f:
        json.dump(hparams, f, indent=4, sort_keys=True)

def main(unused_argv):

    def train_input_fn():
        train_dataset = tf.data.TFRecordDataset(FLAGS.train_dataset)
        train_dataset = train_dataset.map(parser)
        train_dataset = train_dataset.repeat(FLAGS.num_epochs)
        train_dataset = train_dataset.batch(FLAGS.batch_size)
        train_iterator = train_dataset.make_one_shot_iterator()

        features, labels = train_iterator.get_next()
        return features, labels

    def eval_input_fn():
        eval_dataset = tf.data.TFRecordDataset(FLAGS.eval_dataset)
        eval_dataset = eval_dataset.map(parser)
        # eval_dataset = eval_dataset.repeat(FLAGS.num_epochs)
        eval_dataset = eval_dataset.batch(FLAGS.batch_size)
        eval_iterator = eval_dataset.make_one_shot_iterator()
        features, labels = eval_iterator.get_next()
        return features, labels

    steg_classifier = tf.estimator.Estimator(
        model_fn=stg_model_fn, model_dir=FLAGS.model_dir)

    # Train
    steg_classifier.train(input_fn=train_input_fn)

    # Evaluation
    eval_results = steg_classifier.evaluate(input_fn=eval_input_fn)
    print(eval_results)

    tf.logging.info('Saving hyperparameters ...')


if __name__ == "__main__":
    tf.app.run()

我不懂那个1280是怎么来的 明明batch_size只有20


补充layer_module的代码:

def conv_group(inputs, activation, filters, kernel_size, pool_size, strides, pool_padding, abs_layer):
    x = tf.layers.conv2d(
        inputs = inputs,
        filters = filters,
        kernel_size = kernel_size,
        padding = "same")

    if (abs_layer):
        x = tf.abs(x)

    x = tf.layers.batch_normalization(inputs = x)

    if (activation == "relu"):
        x = tf.nn.relu(x)
    elif (activation == "tanh"):
        x = tf.nn.tanh(x)
    print(x)
    x = tf.layers.average_pooling2d(
        inputs = x,
        padding = pool_padding,
        pool_size = pool_size,
        strides = strides)
    print(x)
    return x

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2022-09-14 03:25:11

已解决, reshape的问题。

图片输入时是512X512X1,reshape成64X64(这是错误的用法)之后,由于shape的第一个元素是-1,所以意味着batch_size会改变大小来使得总尺寸不变。 所以batch_size变成了20X(512/64)X(512/64) = 20X8X8 = 1280

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文