0

文字レベルの畳み込みNNを構築しています。私は私は私が修正することはできませんエラーを得続けるためtensorflowで寸法を再構築/サイズを変更する方法を大まかに見当もつかないと思う私は、学習データとしてのサンプルの束を持って、各サンプルは、3640の寸法を有する:Conv Net Tensorflowのレイヤー数:ValueError:新しい変数の形状(logistic_regression/weights)を完全に定義する必要があります

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 

コンベンションモデルが開始されます。

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() ` 
+0

完全なスタックトレースを投稿できますか? – mrry

+0

@mrryご覧ください、私はポストを更新しました。私は 'traceback.format_exc()'を使って(完全な)スタックトレースを取得しました – Oleksandra

+0

@mrryは私の更新で十分ですか?私はここで最初のアドバイスを使用した:http://stackoverflow.com/questions/3702675/how-to-print-the-full-traceback-without-halting-the-program完全なスタックトレースを印刷するが、これはまさにあなたが私に提供したかったものです。 – Oleksandra

答えて

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]が一定値を持っていますが、それはにテンソルpoolの静的な形状を推測するので、TensorFlowのクライアント側定数の畳み込みは、現在、この式を処理しません。 (?, ?)(ログ出力が示すように)です。 tf.TensorShapeオブジェクトではなくtf.Tensorオブジェクトとして、代わりtf.shape(pool)pool.get_shape()poolの(部分的に定義された)形状に関するTensorFlowもう少し情報を与える使用

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

:一つの回避策は、次の変更を行うことです。この変更後、pool_shape[2]pool_shape[3]には既知の値があるため、poolの列の数がわかります。

+0

このアプローチでは、 "Int32が必要ですが次元があります"という行に沿ってエラーが発生しました。最終的に、私は明示的にコンバートの外の変数にサンプル数を格納しました。モデルを作成し、それを周回するなどして、最高の解決策ではなく、今のところうまくいくでしょう。ご意見ありがとうございます.Tensorflowの本当の開発者が問題に対応していることを確認するのは素晴らしいことです:) – Oleksandra

+1

ええ、ここでは自動タイプ変換が少し弱い可能性があります。動作するはずのアップデートを投稿しました - もしそうなら私にお知らせください! – mrry

+0

はい、うまくいきます! – Oleksandra

関連する問題