我想将共同出现作为损失函数的先验知识。 像这样的共生矩阵, enter image description here
我的网络是这样的,
# x = tf.placeholder(tf.float32, [None, 745])
x = tf.placeholder(tf.float32)
W1 = tf.Variable(tf.truncated_normal([2056, 500], stddev=0.1))
b1 = tf.Variable(tf.zeros([500]))
layer1 = tf.nn.relu(tf.matmul(x, W1) + b1)#隐层
# layer1 = tf.clip_by_value(tf.matmul(x, W1) + b1)#隐层
W2 = tf.Variable(tf.truncated_normal([500,33], stddev=0.1))
b2 = tf.Variable(tf.zeros([33]))
# layer2 = tf.nn.tanh(tf.matmul(layer1, W2) + b2)
# W3 = tf.Variable(tf.truncated_normal([500, 11], stddev=0.1))
# b3 = tf.Variable(tf.zeros([11]))
y_without=tf.matmul(layer1, W2) + b2
y = tf.nn.sigmoid(y_without)#输出层,sigmoid函数
# y_ = tf.placeholder(tf.float32, [None, 101])
y_ = tf.placeholder(tf.float32)
我想计算W2的每两列之间的相似性,计算函数如下,
def frob(w1,w2):
asa=(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(w1,w2)))))
return(asa)
def occu(W2):
each_mul=[]
# total_occu=tf.Variable(tf.constant(0.0))
for i in range(33):
for j in range(33):
num=float(co[i,j])
frob_pair=frob((W2)[:,i],(W2)[:,j])
mul=tf.multiply(num,frob_pair)
each_mul.append(mul)
wer=tf.reduce_sum(each_mul)
wer/=40000
# print(each_mul)
return wer#此时p等于0.01
像这样训练一步,
cross_entropy=tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y_,logits=y_without))
coco=occu(W2)
loss = tf.add(cross_entropy , coco)
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(loss)
examples, labels = input_pipeline(200,20)
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
sess = tf.Session()
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
train_accu=[]
test_accu=[]
train_loss=[]
saver=tf.train.Saver()
now=time.time()
try:
i = 0
while not coord.should_stop():
i = i + 1
example_batch, label_batch = sess.run([examples, labels])
# aa=sess.run(example_batch)
# print(example_batch)
# break
print(sess.run(coco))
sess.run(train_step, feed_dict={x: example_batch, y_: label_batch})
print(sess.run(coco))
train_loss.append(sess.run(loss,feed_dict={x:example_batch,y_:label_batch}))
train_accu.append(hit3accu(sess.run(y,feed_dict={x:example_batch}),label_batch))
test_accu.append(hit3accu(sess.run(y,feed_dict={x:X_test_final_accu}),y_test_accu))
# print(np.sum((label_batch[:,18])))
if i % 1 == 0:
# print(sess.run(occu(W2)))
# summary = sess.run(merged, feed_dict={x: example_batch, y_: label_batch})
print('iter:%s,train_loss:%s,train_accu:%s,test_accu:%s'%(i,train_loss[-1],train_accu[-1],test_accu[-1]))
if i %1000==0:
model_path = "F:\co-occurence\saved\model{}.ckpt".format(i)
saver.save(sess,model_path)
# trainwriter.add_summary(summary,i)
# print(i)
# break
except tf.errors.OutOfRangeError:
print('Done training -- epoch limit reached')
finally:
# When done, ask the threads to stop.
coord.request_stop()
# Wait for threads to finish.
coord.join(threads)
duration=time.time()-now
print(duration)
然而,我得到了错误 enter image description here 似乎在第一个W2被正确初始化,但是在一个时代之后W2变成了南。 如果有人能给我一些关于这个问题的想法,我将非常感激。