神经网络有时收敛到 0.5

如何解决神经网络有时收敛到 0.5

感谢您抽出宝贵时间。

我编写了一个带有随机输入的神经网络。网络试图对“图片”中的 5 个符号之一进行分类,该图片是一个 3x3 网格,我在 (0,1) 之间给出了随机值以指示“像素”的“亮度”。

有时神经网络会收敛到 0.5(误差)。当我提高学习率时,这总是发生(learning_rate > 0.5)。例如,当 learning_rate 为 0.02 时,网络收敛到 0 错误。

你们中的任何人都可以看看我是否做错了什么吗?

非常感谢!

# -*- coding: utf-8 -*-
"""
Created on Tue Jan  5 23:55:49 2021

@author: Stein
"""

import numpy as np 
import matplotlib.pyplot as plt
from tqdm import tqdm
import math as m

def derivative_c(x):
    return x

def derivative_sigmoid(x):
  
    return sigmoid(x) * (1 - sigmoid(x))
    
def sigmoid(x):

    return 1/(1+np.exp(-1*x))


sim_cons = 5000
n_of_input_neurons = 9
n_of_output_neurons = 5
training_data = np.random.rand(sim_cons,n_of_input_neurons)
brightness_matrix = np.zeros((sim_cons,n_of_input_neurons))
truth_vector = np.zeros((sim_cons,n_of_output_neurons))
rows = training_data.shape[0]
cols = training_data.shape[1]

weight_matrix = np.random.uniform(-1,1,(n_of_input_neurons+1,n_of_output_neurons))
output_layer = np.zeros((n_of_output_neurons,1))
weights_init = weight_matrix

n__o_sq = 0
n__o_cr = 0
n__o_div = 0
n__o_backsl = 0
n__o_min = 0





for i in range(rows):
    for j in range(cols):
        brightness_matrix[i][j] = m.ceil(training_data[i][j]*4)
    if brightness_matrix[i][0] <= 3 and brightness_matrix[i][1] > 3 and brightness_matrix[i][2] <= 3 and brightness_matrix[i][3] > 3 and brightness_matrix[i][4] > 3 and brightness_matrix[i][5] > 3 and brightness_matrix[i][6] <= 3 and brightness_matrix[i][7] > 3 and brightness_matrix[i][8] <= 3 or brightness_matrix[i][0] <= 2 and brightness_matrix[i][1] > 2 and brightness_matrix[i][2] <= 2 and brightness_matrix[i][3] > 2 and brightness_matrix[i][4] > 2 and brightness_matrix[i][5] > 2 and brightness_matrix[i][6] <= 2 and brightness_matrix[i][7] > 2 and brightness_matrix[i][8] <= 2 or brightness_matrix[i][0] <= 1 and brightness_matrix[i][1] > 1 and brightness_matrix[i][2] <= 1 and brightness_matrix[i][3] > 1 and brightness_matrix[i][4] > 1 and brightness_matrix[i][5] > 1 and brightness_matrix[i][6] <= 1 and brightness_matrix[i][7] > 1 and brightness_matrix[i][8] <= 1:
        n__o_sq += 1
        truth_vector[i][0] = 1
    elif brightness_matrix[i][0] > 3 and brightness_matrix[i][1] <= 3 and brightness_matrix[i][2] > 3 and brightness_matrix[i][3] <= 3 and brightness_matrix[i][4] > 3 and brightness_matrix[i][5] <= 3 and brightness_matrix[i][6] > 3 and brightness_matrix[i][7] <= 3 and brightness_matrix[i][8] > 3 or brightness_matrix[i][0] > 2 and brightness_matrix[i][1] <= 2 and brightness_matrix[i][2] > 2 and brightness_matrix[i][3] <= 2 and brightness_matrix[i][4] > 2 and brightness_matrix[i][5] <= 2 and brightness_matrix[i][6] > 2 and brightness_matrix[i][7] <= 2 and brightness_matrix[i][8] > 2 or brightness_matrix[i][0] > 1 and brightness_matrix[i][1] <= 1 and brightness_matrix[i][2] > 1 and brightness_matrix[i][3] <= 1 and brightness_matrix[i][4] > 1 and brightness_matrix[i][5] <= 1 and brightness_matrix[i][6] > 1 and brightness_matrix[i][7] <= 1 and brightness_matrix[i][8] > 1:
        n__o_cr += 1
        truth_vector[i][1] = 1
    elif brightness_matrix[i][0] <= 3 and brightness_matrix[i][1] <= 3 and brightness_matrix[i][2] > 3 and brightness_matrix[i][3] <= 3 and brightness_matrix[i][4] > 3 and brightness_matrix[i][5] <= 3 and brightness_matrix[i][6] > 3 and brightness_matrix[i][7] <= 3 and brightness_matrix[i][8] <= 3 or brightness_matrix[i][0] <= 2 and brightness_matrix[i][1] <= 2 and brightness_matrix[i][2] > 2 and brightness_matrix[i][3] <= 2 and brightness_matrix[i][4] > 2 and brightness_matrix[i][5] <= 2 and brightness_matrix[i][6] > 2 and brightness_matrix[i][7] <= 2 and brightness_matrix[i][8] <= 2 or brightness_matrix[i][0] <= 1 and brightness_matrix[i][1] <= 1 and brightness_matrix[i][2] > 1 and brightness_matrix[i][3] <= 1 and brightness_matrix[i][4] > 1 and brightness_matrix[i][5] <= 1 and brightness_matrix[i][6] > 1 and brightness_matrix[i][7] <= 1 and brightness_matrix[i][8] <= 1:
        n__o_div += 1
        truth_vector[i][2] = 1        
    elif brightness_matrix[i][0] > 3 and brightness_matrix[i][1] <= 3 and brightness_matrix[i][2] <= 3 and brightness_matrix[i][3] <= 3 and brightness_matrix[i][4] > 3 and brightness_matrix[i][5] <= 3 and brightness_matrix[i][6] <= 3 and brightness_matrix[i][7] <= 3 and brightness_matrix[i][8] > 3 or brightness_matrix[i][0] > 2 and brightness_matrix[i][1] <= 2 and brightness_matrix[i][2] <= 2 and brightness_matrix[i][3] <= 2 and brightness_matrix[i][4] > 2 and brightness_matrix[i][5] <= 2 and brightness_matrix[i][6] <= 2 and brightness_matrix[i][7] <= 2 and brightness_matrix[i][8] > 2 or brightness_matrix[i][0] > 1 and brightness_matrix[i][1] <= 1 and brightness_matrix[i][2] <= 1 and brightness_matrix[i][3] <= 1 and brightness_matrix[i][4] > 1 and brightness_matrix[i][5] <= 1 and brightness_matrix[i][6] <= 1 and brightness_matrix[i][7] <= 1 and brightness_matrix[i][8] > 1:
        n__o_backsl += 1
        truth_vector[i][3] = 1
    elif brightness_matrix[i][0] <= 3 and brightness_matrix[i][1] <= 3 and brightness_matrix[i][2] <= 3 and brightness_matrix[i][3] > 3 and brightness_matrix[i][4] > 3 and brightness_matrix[i][5] > 3 and brightness_matrix[i][6] <= 3 and brightness_matrix[i][7] <= 3 and brightness_matrix[i][8] <= 3 or brightness_matrix[i][0] <= 2 and brightness_matrix[i][1] <= 2 and brightness_matrix[i][2] <= 2 and brightness_matrix[i][3] > 2 and brightness_matrix[i][4] > 2 and brightness_matrix[i][5] > 2 and brightness_matrix[i][6] <= 2 and brightness_matrix[i][7] <= 2 and brightness_matrix[i][8] <= 2 or brightness_matrix[i][0] <= 1 and brightness_matrix[i][1] <= 1 and brightness_matrix[i][2] <= 1 and brightness_matrix[i][3] > 1 and brightness_matrix[i][4] > 1 and brightness_matrix[i][5] > 1 and brightness_matrix[i][6] <= 1 and brightness_matrix[i][7] <= 1 and brightness_matrix[i][8] <= 1:
        n__o_min += 1
        truth_vector[i][4] = 1
        
## remove noise samples
print(n__o_sq) 
print(n__o_cr) 
print(n__o_div) 
print(n__o_backsl) 
print(n__o_min) 
real_training_data = np.where(truth_vector > 0)
range_1 = len(real_training_data[0])
print(range_1)
truth_vector_new = np.zeros((range_1,5))
j=0
training_data_new = np.zeros((range_1,n_of_input_neurons))
for i in range(len(truth_vector)):
    if sum(truth_vector[i]) > 0:
        truth_vector_new[j] = truth_vector[i]        
        training_data_new[j] = training_data[i]
        j+=1
bias_array = np.ones((range_1,1))        
  
training_data_new = np.hstack([training_data_new,bias_array])



def forward_prop(weight_matrix,k):

    forward_sum = 0
    rows = weight_matrix.shape[0]
    cols = weight_matrix.shape[1]
    forward_sum_array = np.zeros((n_of_output_neurons,1))
    error_array = np.zeros((n_of_output_neurons,1))
    
    for i in range(cols):

        forward_sum = 0
        
        for j in range(rows):
    
            forward_sum += float(training_data_new[k][j] * weight_matrix[j][i])
        
        forward_sum_array[i] = forward_sum
        
        output_layer[i] = sigmoid(forward_sum)

    for i in range(n_of_output_neurons):

        error_array[i] = (0.5 * (output_layer[i] - truth_vector_new[k][i])**2)
    
    return forward_sum_array,sum(error_array),error_array







derivatives_total = np.zeros((n_of_input_neurons+1,n_of_output_neurons))



def backward_prop(weight_matrix,derivatives_total,k):
     
    truth_vector_temp = np.matrix.transpose(truth_vector_new)
    der_1_v = np.zeros((n_of_output_neurons,1))
    for i in range(len(der_1_v)):
        der_1_v[i] = output_layer[i] - truth_vector_temp[i][k]
    derivative_cost = derivative_c(der_1_v)
    derivative_2 = derivative_sigmoid(forward_sum_array)
    
    final_derivative = derivative_cost * derivative_2

    derivatives = np.zeros((n_of_input_neurons+1,n_of_output_neurons))

    rows = derivatives.shape[0]
    cols = derivatives.shape[1]

    for i in range(cols):
        for j in range(rows):
            derivatives[j][i] = final_derivative[i] * training_data_new[k][j]
    derivatives_total += derivatives

    return weight_matrix,error,derivatives_total









error = 100
counter = []
error_array  = []
i = 0



while error >= 0.00000001:
    for k in range(range_1):
        forward_sum_array,a = forward_prop(weight_matrix,k)
        
        weight_matrix,derivatives_total = backward_prop(weight_matrix,k)
    weight_matrix = weight_matrix - 0.0001 * derivatives_total
    print(error)
    error_array.append(error)
    counter.append(i)
    i+=1
    plt.plot(counter,error_array)
    plt.show()

else:
    print('done')
    print('')
    print('input was')
    print(training_data_new)
    print('truth was')
    print(truth_vector_new)
    print('weights init')
    print(weights_init)
    print('weights end')
    print(weight_matrix)
correct_counter = 0
for k in range(range_1):    

    forward_sum_array,a  = forward_prop(weight_matrix,k)

    print(output_layer)
    print(truth_vector_new[k])
    print(np.argmax(truth_vector_new[k]))
    print(np.argmax(output_layer))
    if np.argmax(truth_vector_new[k]) == np.argmax(output_layer):
                 correct_counter += 1
    print(correct_counter/range_1)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res