【问题标题】:Dimentions of layers in Conv Net Tensorflow: ValueError: Shape of a new variable (logistic_regression/weights) must be fully definedConv Net Tensorflow 中层的维度:ValueError:必须完全定义新变量的形状(逻辑回归/权重)
【发布时间】:2016-10-09 19:42:13
【问题描述】:

我正在构建一个字符级卷积神经网络。我有一堆样本作为训练数据,每个样本的尺寸为 3640。我想我大致不知道如何在 tensorflow 中调整尺寸/重塑尺寸,因为我不断收到无法修复的错误:

Traceback (most recent call last):
  File "/Users/osopova/Documents/00_KSU_Masters/00_2016_Fall/00_Research/cnn_da/step_4_cnn_4.py", line 87, in my_conv_model
    prediction, loss = learn.models.logistic_regression(pool, y)
  File "/Users/osopova/Applications/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/models.py", line 146, in logistic_regression
    'weights', [x.get_shape()[1], y.get_shape()[-1]], dtype=dtype)
  File "/Users/osopova/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 873, in get_variable
    custom_getter=custom_getter)
  File "/Users/osopova/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 700, in get_variable
    custom_getter=custom_getter)
  File "/Users/osopova/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 217, in get_variable
    validate_shape=validate_shape)
  File "/Users/osopova/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 202, in _true_getter
    caching_device=caching_device, validate_shape=validate_shape)
  File "/Users/osopova/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 515, in _get_single_variable
    "but instead was %s." % (name, shape))
ValueError: Shape of a new variable (logistic_regression/weights) must be fully defined, but instead was (?, 1).
Traceback (most recent call last):
  File "/Users/osopova/Documents/00_KSU_Masters/00_2016_Fall/00_Research/cnn_da/step_4_cnn_4.py", line 175, in <module>
Traceback (most recent call last):
  File "/Users/osopova/Documents/00_KSU_Masters/00_2016_Fall/00_Research/cnn_da/step_4_cnn_4.py", line 87, in my_conv_model
    prediction, loss = learn.models.logistic_regression(pool, y)
  File "/Users/osopova/Applications/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/models.py", line 146, in logistic_regression
    'weights', [x.get_shape()[1], y.get_shape()[-1]], dtype=dtype)
  File "/Users/osopova/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 873, in get_variable
    custom_getter=custom_getter)
  File "/Users/osopova/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 700, in get_variable
    custom_getter=custom_getter)
  File "/Users/osopova/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 217, in get_variable
    validate_shape=validate_shape)
  File "/Users/osopova/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 202, in _true_getter
    caching_device=caching_device, validate_shape=validate_shape)
  File "/Users/osopova/Applications/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 515, in _get_single_variable
    "but instead was %s." % (name, shape))
ValueError: Shape of a new variable (logistic_regression/weights) must be fully defined, but instead was (?, 1).

代码如下:

import tensorflow as tf
from tensorflow.contrib import learn

N_FEATURES = 140*26
N_FILTERS = 10
WINDOW_SIZE = 3

Conv 模型开始:

def my_conv_model(x, y):

# to form a 4d tensor of shape batch_size x 1 x N_FEATURES x 1
x = tf.reshape(x, [-1, 1, N_FEATURES, 1])

# this will give sliding window of 1 x WINDOW_SIZE convolution.
features = tf.contrib.layers.convolution2d(inputs=x,
                                           num_outputs=N_FILTERS,
                                           kernel_size=[1, WINDOW_SIZE],
                                           padding='VALID')

# Add a RELU for non linearity.
features = tf.nn.relu(features)

# Max pooling across output of Convolution+Relu.
pool = tf.nn.max_pool(features, ksize=[1, 1, 2, 1],
                         strides=[1, 1, 2, 1], padding='SAME')

print("(1) pool_shape", pool.get_shape()) 
print("(1) y_shape", y.get_shape()) 

pool_shape = tf.shape(pool)
pool = tf.reshape(pool, [pool_shape[0], pool_shape[2]*pool_shape[3]])
y = tf.expand_dims(y, 1)

print("(2) pool_shape", pool.get_shape()) 
print("(2) y_shape", y.get_shape()) 

try:
    exc_info = sys.exc_info()

    print("(3) pool_shape", pool.get_shape())
    print("(3) y_shape", y.get_shape())
错误来了:
    prediction, loss = learn.models.logistic_regression(pool, y)
    return prediction, loss
except Exception:
    #print(traceback.format_exc())
    pass
finally:
    # Display the *original* exception
    traceback.print_exception(*exc_info)
    del exc_info
#return prediction, loss

形状:

(1) pool_shape (?, 1, 1819, 10)
(1) y_shape (?,)
(2) pool_shape (?, ?)
(2) y_shape (?, 1)
(3) pool_shape (?, ?)
(3) y_shape (?, 1)

主要:

def main(unused_argv):

    # training and testing data encoded as one-hot
    data_folder = './data'

    sandyData = np.loadtxt(data_folder+'/sandyData.csv', delimiter=',')
    sandyLabels = np.loadtxt(data_folder+'/sandyLabels.csv', delimiter=',')

    x_train, x_test, y_train, y_test = \
        train_test_split(sandyData, sandyLabels, test_size=0.2, random_state=7)

    x_train = np.array(x_train, dtype=np.float32)
    x_test = np.array(x_test, dtype=np.float32)
    y_train = np.array(y_train, dtype=np.float32)
    y_test = np.array(y_test, dtype=np.float32)

    # Build model
    classifier = learn.Estimator(model_fn=my_conv_model)

    # Train and predict
    classifier.fit(x_train, y_train, steps=100)
    y_predicted = [p['class'] for p in classifier.predict(x_test, as_iterable=True)]
    score = metrics.accuracy_score(y_test, y_predicted)
    print('Accuracy: {0:f}'.format(score))


if __name__ == '__main__':
    tf.app.run() `

【问题讨论】:

  • 你能发布完整的堆栈跟踪吗?
  • @mrry 请看一下,我已经更新了帖子。我使用traceback.format_exc() 获取(完整)堆栈跟踪
  • @mrry 我的更新够了吗?我在这里使用了第一个建议:stackoverflow.com/questions/3702675/… 打印完整的堆栈跟踪,但我不确定这是否正是您想要我提供的。
  • 堆栈跟踪很棒,谢谢!但我还需要一点信息:您能否在致电logistic_regression() 之前立即打印y.get_shape() 的值?
  • @mrry 谢谢!!请查看更新。基本上,y 形状是 (?, 1)。

标签: machine-learning tensorflow deep-learning logistic-regression conv-neural-network


【解决方案1】:

看起来问题在于logistic_regression()pool 参数没有已知数量的列。 linear_regression() 需要知道其 x 参数中的列数才能创建适当大小的权重矩阵。

这个问题源于以下一行:

pool_shape = tf.shape(pool)
pool = tf.reshape(pool, [pool_shape[0], pool_shape[2]*pool_shape[3]])

虽然 pool_shape[2]*pool_shape[3] 有一个常量值,但 TensorFlow 的客户端常量折叠当前不处理此表达式,因此它推断张量 pool 的静态形状为 (?, ?)(如您的日志输出所示)。一种解决方法是进行以下更改:

pool_shape = pool.get_shape()
pool = tf.reshape(pool, [-1, (pool_shape[2] * pool_shape[3]).value])

使用pool.get_shape() 而不是tf.shape(pool) 可以为TensorFlow 提供更多关于pool 形状(部分定义)的信息,作为tf.TensorShape 对象而不是tf.Tensor 对象。更改后,pool_shape[2]pool_shape[3] 都具有已知值,因此 pool 中的列数将是已知的。

【讨论】:

  • 使用这种方法时,我得到了一个错误,类似于“Expected int32 but got Dimension”。最终,我最终明确地将样本数量存储在 conv 之外的变量中。模型,然后将其传递以重塑等。这根本不是最好的解决方案,但现在可以使用。感谢您的反馈,很高兴看到来自 Tensorflow 的真正开发人员对问题做出回应 :)
  • 嗯,这里的自动类型转换可能有点弱。我发布了一个应该有效的更新 - 如果有效,请告诉我!
猜你喜欢
  • 2019-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多