TensorFlow conv2d未收敛到预期结果

时间:2017-09-15 19:32:22

标签: python tensorflow conv-neural-network

作为我完全理解张量流的一部分,我设计了简单的实验来验证我对正在发生的事情的理解。

我正在尝试训练一个小天使来生成输入的缩放版本,系统看起来像这样:

enter image description here

训练数据是我在shape=[1, 1, n, 1]的MATLAB中创建的向量,如下所示:

input  =         [a, a, a, ..., a]
output =  0.25 * [a, a, a, ..., a]

我正在训练的过滤器内核,W属于shape=[1, 1, 1, 1]

我会想象W的值应该训练到0.25,但它会训练到0.189。同样,当我创建初始比例因子为0.5的输入和输出时,系统训练为0.378。

我很困惑。 为什么过滤器会达到预期值的三分之二

这是我的代码
I / O生成(MATLAB)

data = [];
numPts = 500;
for indx = 0:19999
    data(end+1).x = rand(1,1).*ones(1,numPts);
    data(end).y = .25 * data(end).x;
end

模型和训练(python tensorflow)
[我删除了我的输入功能以减小帖子的大小,但我确信它正在工作]

tf.app.flags.DEFINE_integer('max_steps', 50000,"""Number of batches to run.""")
tf.app.flags.DEFINE_integer('num_samples', 500,"""Samples per vect.""")
tf.app.flags.DEFINE_integer('filter_size', 1,"""Size of filter.""")

with tf.Graph().as_default():
    global_step = tf.contrib.framework.get_or_create_global_step()

    d1, d2 = inputs()

    # Placeholders
    X = tf.placeholder(tf.float32, name="X")
    Y = tf.placeholder(tf.float32, shape=(1, 1, FLAGS.num_samples, 1), name="Y")

    # TRAINABLE FILTER KERNEL
    W = tf.Variable(tf.random_normal([1, FLAGS.filter_size, 1, 1]), dtype=tf.float32)

    # MODEL
    x = tf.reshape(X, shape=[1, 1, FLAGS.num_samples, 1])
    y_predicted = tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

    # LOSS AND STUFF
    Y = tf.reshape(Y, shape=[1, 1, FLAGS.num_samples])
    y_p = tf.reshape(y_predicted, shape=[1, 1, FLAGS.num_samples])

    loss = tf.losses.mean_squared_error(labels=Y, predictions=y_p)

    opt = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())

    with tf.Session() as sess:

        sess.run(init_op)

        tf.train.start_queue_runners(coord=tf.train.Coordinator())

        loss_vals, w_vals = [], [] # for storage

        for n in range(0, FLAGS.max_steps):
            x_data = sess.run(d1)
            y_data = sess.run(d2)

            sess.run(opt, feed_dict={
                             X: x_data,
                             Y: y_data
                         })

            # DISPLAY PROGRESS
            if n % 500 == 0:
                print(n)

            # STORE LOSS AND FILTER
            if n % 100 == 0:
                w_vals.append(sess.run([W]))
                loss_vals.append(
                    sess.run(loss, feed_dict={
                        X: x_data,
                        Y: y_data
                    }))

        # SAVE TO MATLAB
        sio.savemat('./params.mat', {'loss': loss_vals, 'w': w_vals})

我做过的事情

  1. 我已经验证过,如果你为某个值为0.25的东西进行卷积,它会将其缩放0.25
  2. 我尝试了很多损失。 huber_lossabsolute_differencetf.square(Y - y_p)。总是~2 / 3太低了。
  3. 我玩过优化器。 AdamOptimizerGradientDescentOptimizer。改变了学习率。总是~2 / 3太低了。
  4. 制作了一些相当于0.189 (absolute_difference and mean_squared_error)的滤镜重量的漂亮照片。有趣的是,当您放大数据时,张量流似乎重复它每5000步重量值的选择;但这是一个不同日子的故事。

1 个答案:

答案 0 :(得分:0)

对我来说没问题。我已将输入从matlab更改为python.numpy

import tensorflow as tf
import numpy as np

FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_integer('max_steps', 50000, "Number of batches to run.""")
tf.app.flags.DEFINE_integer('num_samples', 500, "Samples per vect.""")
tf.app.flags.DEFINE_integer('filter_size', 1, "Size of filter.""")

with tf.Graph().as_default():
    global_step = tf.contrib.framework.get_or_create_global_step()

    # Placeholders
    X = tf.placeholder(tf.float32, name="X")
    Y = tf.placeholder(tf.float32, shape=(1, 1, FLAGS.num_samples, 1), name="Y")

    # TRAINABLE FILTER KERNEL
    W = tf.Variable(tf.random_normal([1, FLAGS.filter_size, 1, 1]), dtype=tf.float32)

    # MODEL
    x = tf.reshape(X, shape=[1, 1, FLAGS.num_samples, 1])
    y_predicted = tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

    # LOSS AND STUFF
    y = tf.reshape(Y, shape=[1, 1, FLAGS.num_samples])
    y_p = tf.reshape(y_predicted, shape=[1, 1, FLAGS.num_samples])

    loss = tf.losses.mean_squared_error(labels=y, predictions=y_p)

    opt = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())

    with tf.Session() as sess:

        sess.run(init_op)

        tf.train.start_queue_runners(coord=tf.train.Coordinator())

        loss_vals, w_vals = [], [] # for storage

        for n in range(0, FLAGS.max_steps):
            x_data = np.random.normal(size=(1, 1, FLAGS.num_samples, 1))
            y_data = x_data * 0.25

            sess.run(opt, feed_dict={
                             X: x_data,
                             Y: y_data
                         })

            # STORE LOSS AND FILTER
            if n % 100 == 0:
                w_vals.append(sess.run([W]))
                loss_vals.append(
                    sess.run(loss, feed_dict={
                        X: x_data,
                        Y: y_data
                    }))

            # DISPLAY PROGRESS
            if n % 500 == 0:
                print n, loss_vals[-1], w_vals[-1]

,输出如下:

0 0.0106585 [array([[[[ 0.14452107]]]], dtype=float32)]
500 0.00148794 [array([[[[ 0.21119362]]]], dtype=float32)]
1000 0.000211823 [array([[[[ 0.23576953]]]], dtype=float32)]
1500 2.60087e-05 [array([[[[ 0.24478287]]]], dtype=float32)]
2000 3.46493e-06 [array([[[[ 0.24807557]]]], dtype=float32)]
2500 5.06054e-07 [array([[[[ 0.2492941]]]], dtype=float32)]
3000 6.88539e-08 [array([[[[ 0.24974038]]]], dtype=float32)]
3500 8.94391e-09 [array([[[[ 0.2499048]]]], dtype=float32)]
4000 1.31581e-09 [array([[[[ 0.24996498]]]], dtype=float32)]
4500 1.67827e-10 [array([[[[ 0.24998713]]]], dtype=float32)]
5000 2.12909e-11 [array([[[[ 0.24999554]]]], dtype=float32)]

也许您应该检查输入的matlab数据。