当前位置: 首页 > news >正文

公司级做宣传图的网站产品网站建站

公司级做宣传图的网站,产品网站建站,徐州专门做网站,网站你懂我意思正能量晚上在线下载免费软件魅族过拟合泛化性弱 欠拟合解决方法: 增加输入特征项 增加网络参数 减少正则化参数 过拟合的解决方法: 数据清洗 增大训练集 采用正则化 增大正则化参数 正则化缓解过拟合 正则化在损失函数中引入模型复杂度指标,利用给w增加权重,…

过拟合泛化性弱

欠拟合解决方法:

        增加输入特征项

        增加网络参数

        减少正则化参数

过拟合的解决方法:

        数据清洗

        增大训练集

        采用正则化

        增大正则化参数

正则化缓解过拟合

正则化在损失函数中引入模型复杂度指标,利用给w增加权重,弱化数据集的噪声,loss = loss(y与y_) + REGULARIZER*loss(w)

模型中所有参数的损失函数,如交叉上海,均方误差

利用超参数REGULARIZER给出参数w在总loss中的比例,即正则化权重, w是需要正则化的参数

正则化的选择

L1正则化大概率会使很多参数变为0,因此该方法可通过系数参数,减少参数的数量,降低复杂度

L2正则化会使参数很接近0但不为0,因此该方法可通过减少参数值的大小降低复杂度 

with tf.GradientTape() as tape:h1 = tf.matul(x_train, w1) + b1h1 = tf.nn.relu(h1)y = tf.matmul(h1, w2) + b2loss_mse = tf.reduce_mean(tf.square(y_train - y))loss_ragularization = []loss_regularization.append(tf.nn.l2_loss(w1))loss_regularization.append(tf.nn.l2_loss(w2))loss_regularization = tf.reduce_sum(loss_regularization)loss = loss_mse + 0.03 * loss_regularization
variables = [w1, b1, w2, b2】
grads = tape.gradient(loss, variables)

生成网格覆盖这些点,会对每个坐标生成一个预测值,输出预测值为0.5的连成线,这个线就是红点和蓝点的分界线。

# 导入所需模块
import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd# 读入数据/标签 生成x_train y_train
df = pd.read_csv('dot.csv')
x_data = np.array(df[['x1', 'x2']])
y_data = np.array(df['y_c'])x_train = x_data
y_train = y_data.reshape(-1, 1)Y_c = [['red' if y else 'blue'] for y in y_train]# 转换x的数据类型,否则后面矩阵相乘时会因数据类型问题报错
x_train = tf.cast(x_train, tf.float32)
y_train = tf.cast(y_train, tf.float32)# from_tensor_slices函数切分传入的张量的第一个维度,生成相应的数据集,使输入特征和标签值一一对应
train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32)# 生成神经网络的参数,输入层为4个神经元,隐藏层为32个神经元,2层隐藏层,输出层为3个神经元
# 用tf.Variable()保证参数可训练
w1 = tf.Variable(tf.random.normal([2, 11]), dtype=tf.float32)
b1 = tf.Variable(tf.constant(0.01, shape=[11]))w2 = tf.Variable(tf.random.normal([11, 1]), dtype=tf.float32)
b2 = tf.Variable(tf.constant(0.01, shape=[1]))lr = 0.005  # 学习率为
epoch = 800  # 循环轮数# 训练部分
for epoch in range(epoch):for step, (x_train, y_train) in enumerate(train_db):with tf.GradientTape() as tape:  # 记录梯度信息h1 = tf.matmul(x_train, w1) + b1  # 记录神经网络乘加运算h1 = tf.nn.relu(h1)y = tf.matmul(h1, w2) + b2# 采用均方误差损失函数mse = mean(sum(y-out)^2)loss_mse = tf.reduce_mean(tf.square(y_train - y))# 添加l2正则化loss_regularization = []# tf.nn.l2_loss(w)=sum(w ** 2) / 2loss_regularization.append(tf.nn.l2_loss(w1))loss_regularization.append(tf.nn.l2_loss(w2))# 求和# 例:x=tf.constant(([1,1,1],[1,1,1]))#   tf.reduce_sum(x)# >>>6loss_regularization = tf.reduce_sum(loss_regularization)loss = loss_mse + 0.03 * loss_regularization  # REGULARIZER = 0.03# 计算loss对各个参数的梯度variables = [w1, b1, w2, b2]grads = tape.gradient(loss, variables)# 实现梯度更新# w1 = w1 - lr * w1_gradw1.assign_sub(lr * grads[0])b1.assign_sub(lr * grads[1])w2.assign_sub(lr * grads[2])b2.assign_sub(lr * grads[3])# 每200个epoch,打印loss信息if epoch % 20 == 0:print('epoch:', epoch, 'loss:', float(loss))# 预测部分
print("*******predict*******")
# xx在-3到3之间以步长为0.01,yy在-3到3之间以步长0.01,生成间隔数值点
xx, yy = np.mgrid[-3:3:.1, -3:3:.1]
# 将xx, yy拉直,并合并配对为二维张量,生成二维坐标点
grid = np.c_[xx.ravel(), yy.ravel()]
grid = tf.cast(grid, tf.float32)
# 将网格坐标点喂入神经网络,进行预测,probs为输出
probs = []
for x_predict in grid:# 使用训练好的参数进行预测h1 = tf.matmul([x_predict], w1) + b1h1 = tf.nn.relu(h1)y = tf.matmul(h1, w2) + b2  # y为预测结果probs.append(y)# 取第0列给x1,取第1列给x2
x1 = x_data[:, 0]
x2 = x_data[:, 1]
# probs的shape调整成xx的样子
probs = np.array(probs).reshape(xx.shape)
plt.scatter(x1, x2, color=np.squeeze(Y_c))
# 把坐标xx yy和对应的值probs放入contour函数,给probs值为0.5的所有点上色  plt.show()后 显示的是红蓝点的分界线
plt.contour(xx, yy, probs, levels=[.5])
plt.show()# 读入红蓝点,画出分割线,包含正则化
# 不清楚的数据,建议print出来查看

存在过拟合现象,轮廓不够平滑, 使用l2正则化缓解过拟合

# 导入所需模块
import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd# 读入数据/标签 生成x_train y_train
df = pd.read_csv('dot.csv')
x_data = np.array(df[['x1', 'x2']])
y_data = np.array(df['y_c'])x_train = x_data
y_train = y_data.reshape(-1, 1)Y_c = [['red' if y else 'blue'] for y in y_train]# 转换x的数据类型,否则后面矩阵相乘时会因数据类型问题报错
x_train = tf.cast(x_train, tf.float32)
y_train = tf.cast(y_train, tf.float32)# from_tensor_slices函数切分传入的张量的第一个维度,生成相应的数据集,使输入特征和标签值一一对应
train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32)# 生成神经网络的参数,输入层为4个神经元,隐藏层为32个神经元,2层隐藏层,输出层为3个神经元
# 用tf.Variable()保证参数可训练
w1 = tf.Variable(tf.random.normal([2, 11]), dtype=tf.float32)
b1 = tf.Variable(tf.constant(0.01, shape=[11]))w2 = tf.Variable(tf.random.normal([11, 1]), dtype=tf.float32)
b2 = tf.Variable(tf.constant(0.01, shape=[1]))lr = 0.005  # 学习率为
epoch = 800  # 循环轮数# 训练部分
for epoch in range(epoch):for step, (x_train, y_train) in enumerate(train_db):with tf.GradientTape() as tape:  # 记录梯度信息h1 = tf.matmul(x_train, w1) + b1  # 记录神经网络乘加运算h1 = tf.nn.relu(h1)y = tf.matmul(h1, w2) + b2# 采用均方误差损失函数mse = mean(sum(y-out)^2)loss_mse = tf.reduce_mean(tf.square(y_train - y))# 添加l2正则化loss_regularization = []# tf.nn.l2_loss(w)=sum(w ** 2) / 2loss_regularization.append(tf.nn.l2_loss(w1))loss_regularization.append(tf.nn.l2_loss(w2))# 求和# 例:x=tf.constant(([1,1,1],[1,1,1]))#   tf.reduce_sum(x)# >>>6loss_regularization = tf.reduce_sum(loss_regularization)loss = loss_mse + 0.03 * loss_regularization  # REGULARIZER = 0.03# 计算loss对各个参数的梯度variables = [w1, b1, w2, b2]grads = tape.gradient(loss, variables)# 实现梯度更新# w1 = w1 - lr * w1_gradw1.assign_sub(lr * grads[0])b1.assign_sub(lr * grads[1])w2.assign_sub(lr * grads[2])b2.assign_sub(lr * grads[3])# 每200个epoch,打印loss信息if epoch % 20 == 0:print('epoch:', epoch, 'loss:', float(loss))# 预测部分
print("*******predict*******")
# xx在-3到3之间以步长为0.01,yy在-3到3之间以步长0.01,生成间隔数值点
xx, yy = np.mgrid[-3:3:.1, -3:3:.1]
# 将xx, yy拉直,并合并配对为二维张量,生成二维坐标点
grid = np.c_[xx.ravel(), yy.ravel()]
grid = tf.cast(grid, tf.float32)
# 将网格坐标点喂入神经网络,进行预测,probs为输出
probs = []
for x_predict in grid:# 使用训练好的参数进行预测h1 = tf.matmul([x_predict], w1) + b1h1 = tf.nn.relu(h1)y = tf.matmul(h1, w2) + b2  # y为预测结果probs.append(y)# 取第0列给x1,取第1列给x2
x1 = x_data[:, 0]
x2 = x_data[:, 1]
# probs的shape调整成xx的样子
probs = np.array(probs).reshape(xx.shape)
plt.scatter(x1, x2, color=np.squeeze(Y_c))
# 把坐标xx yy和对应的值probs放入contour函数,给probs值为0.5的所有点上色  plt.show()后 显示的是红蓝点的分界线
plt.contour(xx, yy, probs, levels=[.5])
plt.show()# 读入红蓝点,画出分割线,包含正则化
# 不清楚的数据,建议print出来查看

python EmptyDataError No columns to parse from file sites:stackoverflow.com

http://www.15wanjia.com/news/191213.html

相关文章:

  • 网站查询关键词排名软件外贸SOHO建公司网站
  • 西昌有做网站的公司吗国外做家装的网站有哪些
  • 免费下载的网站yeti wordpress
  • 设计在线设计网站室内设计师接单网
  • 做外贸网站报价汉中网站网站建设
  • 盗版视频网站建设费用iis怎么搭建asp网站
  • 怎么在百度上面做网站网站设计用什么做
  • 技术社区网站开发在线做静态头像的网站
  • 网站做app的软件叫什么网站选择语言怎么做
  • 怎么在网上做彩票网站长春微网站
  • 商业网站备案流程如何架设内部网站
  • wordpress网站被自动跳转宁波易通宁波网站建设
  • mvc5网站开发之美搭建网站架构怎么做
  • vps网站管理助手wordpress 分类目录404
  • 一流导航设计网站网站建设公司行业现状
  • 十大免费模板网站微信网站平台怎么建立
  • 商务网站开发的工作任务好模板网站
  • 邯郸市网站建设多少钱app推广多少钱一个
  • 建设网站出什么科目网站内部链接如何进行优化建设
  • 大学网站建设的意义建商城网站多少钱
  • wordpress如何去除底部沈阳seo公司
  • 外贸型网站的特点杭州市建设银行网站
  • 兰州市建设工程招标投标中心网站进入不wordpress
  • 官方网站建设 磐石网络知名网站建设后台管理实训报告
  • 旅游商城网站建设如何进行公司网站的建设
  • 网店推广方案范文东营企业网站排名优化
  • 诚讯通网站做网站的一年能挣多少钱
  • 全影网的网站哪儿做dwordpress 新浪图片
  • 韩国大型门户网站电子商城网站建设项目规划书
  • 装修设计培训机构sem和seo是什么职位