Skip to main content
 首页 » 编程设计

python之为什么我的网络不会学习

2025年05月04日25www_RR

所以我在 tensorflow 中创建了一个卷积网络,但准确率根本不会改变。我试图让它分辨三角形和圆形之间的区别。它们颜色不同,尺寸相似。这是网络的代码。此外,当我尝试使用完全连接的网络时,准确度几乎为 1。

x = tf.placeholder("float", shape=[None, 3072]) 
y_ = tf.placeholder("float", shape=[None, 2]) 
 
W = tf.Variable(tf.zeros([3072, 2])) 
b = tf.Variable(tf.zeros([2])) 
 
y = tf.nn.softmax(tf.matmul(x,W) + b) 
 
def weight_variable(shape): 
    initial = tf.truncated_normal(shape, stddev=0.1) 
    return tf.Variable(initial) 
 
def bias_variable(shape): 
    initial = tf.constant(0.1, shape=shape) 
    return tf.Variable(initial) 
 
def conv2d(x, W): 
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 
 
def max_pool_2x2(x): 
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 
                           strides=[1, 2, 2, 1], padding='SAME') 
 
W_conv1 = weight_variable([4, 4, 3, 32]) 
b_conv1 = bias_variable([32]) 
 
x_image = tf.reshape(x, [-1,32,32,3]) 
 
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 
h_pool1 = max_pool_2x2(h_conv1) 
 
W_conv2 = weight_variable([4, 4, 32, 64]) 
b_conv2 = bias_variable([64]) 
 
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 
h_pool2 = max_pool_2x2(h_conv2) 
 
W_fc1 = weight_variable([8 * 8 * 64, 1024]) 
b_fc1 = bias_variable([1024]) 
 
h_pool2_flat = tf.reshape(h_pool2, [-1, 8*8*64]) 
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 
 
keep_prob = tf.placeholder("float") 
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 
 
W_fc2 = weight_variable([1024, 2]) 
b_fc2 = bias_variable([2]) 
 
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 
 
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv + 1e-9)) 
train_step = tf.train.AdamOptimizer(1e-6).minimize(cross_entropy) 
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
 
self.feedin = np.reshape(self.inlist, (-1, 1, 32, 32, 3)) 
print(self.feedin) 
sess.run(tf.initialize_all_variables()) 
for i in range(10000): 
    j = i%int(self.ent) 
    if i%100 == 0: 
        train_accuracy = accuracy.eval(feed_dict={ 
            x:self.inlist, y_: self.outListm, keep_prob: 1.0}) 
        print("step %d, training accuracy %g"%(i, train_accuracy)) 
    train_step.run(feed_dict={x: self.feedin[j], y_: self.outListm[j], keep_prob: 0.5}) 

这是其中的两张图片。

这就是我用来创建 self.in 的。我已对其进行更改,以便图像的形状保持不变,但问题仍然存在。

name = QtGui.QFileDialog.getOpenFileNames(self, 'Open File') 
fname = [str(each) for each in name] 
flist = [] 
dlist = [] 
self.inlist = [11, 32, 32, 3] 
for n, val in enumerate(name): 
    flist.append(val) 
    img = Image.open(flist[n]) 
    img.load() 
    data = np.asarray(img, dtype = "float32") 
    dlist.append(data) 
self.inlist = np.concatenate([arr[np.newaxis] for arr in dlist]) 

对于 self out,我有一个包含 2 个元素的列表,如果它是三角形,则第一个元素为 2,如果它是圆形,则第二个元素为 2。

请您参考如下方法:

您不能将所有参数都初始化为零(或任何常数),这几乎是几乎所有类型的神经网络的常识。

让我们想象一个最简单的前馈网络,所有权重矩阵都初始化为相同的常数(包括但不只是零),会发生什么?无论您的输入向量是什么,同一层中所有神经元的激活(输出)都是相同的!这绝对不是你想要的。在您的情况下,您将它们全部初始化为零,这使情况变得更糟。因为除了上述缺点之外,ReLU 在零点甚至不可推导。

因此,对您而言,最佳做法是将权重矩阵 (W) 初始化为随机值,以“打破对称性”。您可以通过 random.randn() 来完成它,但是为了获得更好的性能,有很多技巧可以做到这一点,例如 Xavier 初始化、MSRA 初始化等。对于您的情况下的 ReLU 激活函数,一个可能会指导您在所有这些初始化策略中进行选择的是,您最好将权重矩阵初始化为略微正的,以防 ReLU 函数的输入为负,这可能会使 ReLU 单元变为“死”单元(梯度永远为零) .