###前提・実現したいこと
Pythonでマルチラベル分類を試みています.
機械学習として畳み込みネットをTensorflowで実装しました.
入力画像は7×252の大きさで,各ピクセル値が0以上1以下の
[0.1255,0.5251,0.2131,0.1632,0.7372, ... , 0.0562]
のようなfloat型です.
対するターゲットは
[0,1,0,0,1,0,1, ... ,0]
となるような,長さ88のバイナリベクトルです.
要は,入力画像に応じてバイナリベクトルの各要素の値が(独立して)決定されるような畳み込みネットを構築したいので,マルチラベル分類問題だと仮定してコードを記述しました.
###発生している問題・エラーメッセージ
学習を重ねることによってエラー関数は収束(0.62から0.14程度にまで減少)したのですが、実際に学習させたデータを入力して再現を試みると,極端な話,どのような入力に対しても,
[0,0.3,1,0.5,0,...,0.7]
[0,0.3,1,0.5,0,...,0.7]
[0,0.3,1,0.5,0,...,0.7]
[0,0.3,1,0.5,0,...,0.7]
のような,全く同じパターンのバイナリベクトルが出力されてしまいます.
そもそも正論文を参考にネットワークを組んでいるので,畳み込みネットの構造(レイヤー数,活性化関数など)自体は保証されていると考えております.
###該当のソースコード
記述されていない定数や関数に関しては適切なものが設定されていると仮定して下さい.
入力画像(x_input):1×1764(7×252にreshape)
ターゲット(y_):1×88 (バリナリベクトル)
誤差関数(cross_entoropy):tf.nn.sigmoid_cross_entropy_with_logits
出力(Output):出力層にsigmoid
以下のコードでは学習は一回ですが,実際はバリデーションデータを用いて収束が停滞するまで学習を繰り返し,入力データは毎回ランダムに並べ替えています.
python
1def weight_variable(shape): 2 initial = tf.truncated_normal(shape,mean=0,stddev=0.5,dtype=tf.float32) 3 return tf.Variable(initial) 4 5def bias_variable(shape): 6 initial = tf.truncated_normal(shape,mean=0,stddev=0.5,dtype=tf.float32) 7 return tf.Variable(initial) 8 9def conv2d(x, W): 10 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 11 12def max_pool_1x3(x): 13 return tf.nn.max_pool(x, ksize=[1, 1, 3, 1],strides=[1, 1, 3, 1], padding='SAME') 14 15if __name__ == '__main__': 16 17 x = tf.placeholder("float", shape=[None,252*7]) 18 y_ = tf.placeholder("float", shape=[None,88]) 19 sess = tf.InteractiveSession() 20 21 ######ConvNet####### 22 x_input = tf.reshape(x, [-1,7,252,1]) 23 keep_prob = tf.placeholder("float") 24 W_conv1 = weight_variable([w1_x, w1_y, 1, w1_outc]) 25 b_conv1 = bias_variable([w1_outc]) 26 h_conv1 = tf.tanh(conv2d(x_input, W_conv1) + b_conv1) 27 h_pool1 = max_pool_1x3(h_conv1) 28 h_conv1_drop = tf.nn.dropout(h_pool1, keep_prob) 29 30 31 W_conv2 = weight_variable([w2_x, w2_y, w1_outc, w2_outc]) 32 b_conv2 = bias_variable([w2_outc]) 33 h_conv2 = tf.tanh(conv2d(h_conv1_drop, W_conv2) + b_conv2) 34 h_pool2 = max_pool_1x3(h_conv2) 35 h_conv2_drop = tf.nn.dropout(h_pool2, keep_prob) 36 37 ######Fully connected ####### 38 W_fc1 = weight_variable([28*7*50, 1000]) 39 b_fc1 = bias_variable([1000]) 40 41 h_pool2_flat = tf.reshape(h_conv2_drop, [-1,28*7*50]) 42 h_fc1 = tf.sigmoid(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 43 h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 44 45 W_fc2 = weight_variable([1000, 200]) 46 b_fc2 = bias_variable([200]) 47 h_fc2 = tf.sigmoid(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 48 h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob) 49 50 W_fc3 = weight_variable([200, 88]) 51 b_fc3 = bias_variable([88]) 52 y_conv=tf.matmul(h_fc2_drop, W_fc3) + b_fc3 53 54 Sig_y_conv = tf.sigmoid(y_conv) #for output 55 56 #cross entoropy and optimizer 57 cross_entoropy = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=y_conv,labels= y_)) 58 batch = tf.Variable(0) 59 train_step = tf.train.MomentumOptimizer(1e-2,0.9).minimize(cross_entoropy,global_step=batch) 60 61 sess.run(tf.initialize_all_variables()) 62 63 #getdata 64 for datano in range(datanum) : 65 input_data = DataLibrary[datano] 66 target_label = LabelLibrary[datano] 67 if dataconect == 0: 68 train_data = input_data 69 train_label = target_label 70 else : 71 train_data = np.r_[train_data,input_data] 72 train_label = np.r_[train_label,target_label] 73 perm = np.arange(train_data.shape[0]) 74 np.random.shuffle(perm) 75 train_data = train_data[perm] 76 train_label = train_label[perm] 77 78 #training 79 for i in range(math.ceil(train_data.shape[0]/BATCH_SIZE)): 80 batch = mini_batch(train_data, train_label, i) 81 feed_dict = {x: batch[0], y_: batch[1], keep_prob: Drate} 82 train_step.run(feed_dict=feed_dict) 83 84 #Output 85 Output = Sig_y_conv.eval(feed_dict={x: test_data, y_: test_label, keep_prob: 1.0})
###試したこと
重み付けとバイアスの初期値を色々弄ってみました.
OptimizerもADADELTAやAtomに変更して行ってみました.
いずれも効果はありませんでした(そもそもほとんど収束しない).
回答2件
あなたの回答
tips
プレビュー