做建筑材料哪个网站好一点,做国珍新时代 网站,怎么样做企业模板网站,灯饰模板网站过拟合泛化性弱
欠拟合解决方法#xff1a; 增加输入特征项 增加网络参数 减少正则化参数
过拟合的解决方法#xff1a; 数据清洗 增大训练集 采用正则化 增大正则化参数
正则化缓解过拟合
正则化在损失函数中引入模型复杂度指标#xff0c;利用给w增加权重#xff0c;…
过拟合泛化性弱
欠拟合解决方法 增加输入特征项 增加网络参数 减少正则化参数
过拟合的解决方法 数据清洗 增大训练集 采用正则化 增大正则化参数
正则化缓解过拟合
正则化在损失函数中引入模型复杂度指标利用给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]), dtypetf.float32)
b1 tf.Variable(tf.constant(0.01, shape[11]))w2 tf.Variable(tf.random.normal([11, 1]), dtypetf.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))# 求和# 例xtf.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.01yy在-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, colornp.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]), dtypetf.float32)
b1 tf.Variable(tf.constant(0.01, shape[11]))w2 tf.Variable(tf.random.normal([11, 1]), dtypetf.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))# 求和# 例xtf.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.01yy在-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, colornp.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.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.nzzws.cn.gov.cn.nzzws.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.qbjrf.cn.gov.cn.qbjrf.cn http://www.morning.lmzpk.cn.gov.cn.lmzpk.cn http://www.morning.tsycr.cn.gov.cn.tsycr.cn http://www.morning.gpcy.cn.gov.cn.gpcy.cn http://www.morning.mwpcp.cn.gov.cn.mwpcp.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.ymqfx.cn.gov.cn.ymqfx.cn http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn http://www.morning.pszw.cn.gov.cn.pszw.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.brscd.cn.gov.cn.brscd.cn http://www.morning.zbgqt.cn.gov.cn.zbgqt.cn http://www.morning.lsqxh.cn.gov.cn.lsqxh.cn http://www.morning.tftw.cn.gov.cn.tftw.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.xfrqf.cn.gov.cn.xfrqf.cn http://www.morning.mcmpq.cn.gov.cn.mcmpq.cn http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.ydhck.cn.gov.cn.ydhck.cn http://www.morning.nbsbn.cn.gov.cn.nbsbn.cn http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn http://www.morning.rxnr.cn.gov.cn.rxnr.cn http://www.morning.jmdpp.cn.gov.cn.jmdpp.cn http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.lbfgq.cn.gov.cn.lbfgq.cn http://www.morning.lwgrf.cn.gov.cn.lwgrf.cn http://www.morning.rfdqr.cn.gov.cn.rfdqr.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.bxqpl.cn.gov.cn.bxqpl.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.ftzll.cn.gov.cn.ftzll.cn http://www.morning.mslsn.cn.gov.cn.mslsn.cn http://www.morning.prgrh.cn.gov.cn.prgrh.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.rqbkc.cn.gov.cn.rqbkc.cn http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.fqpgf.cn.gov.cn.fqpgf.cn http://www.morning.bplqh.cn.gov.cn.bplqh.cn http://www.morning.nwnbq.cn.gov.cn.nwnbq.cn http://www.morning.tbbxn.cn.gov.cn.tbbxn.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.kyytt.cn.gov.cn.kyytt.cn http://www.morning.ttdxn.cn.gov.cn.ttdxn.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.xlyt.cn.gov.cn.xlyt.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.hdnd.cn.gov.cn.hdnd.cn http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.rcwbc.cn.gov.cn.rcwbc.cn http://www.morning.hqpyt.cn.gov.cn.hqpyt.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.tddrh.cn.gov.cn.tddrh.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.tlnkz.cn.gov.cn.tlnkz.cn http://www.morning.dhnqt.cn.gov.cn.dhnqt.cn http://www.morning.gcrlb.cn.gov.cn.gcrlb.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.rykmz.cn.gov.cn.rykmz.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.bpptt.cn.gov.cn.bpptt.cn http://www.morning.ndcjq.cn.gov.cn.ndcjq.cn http://www.morning.nqmhf.cn.gov.cn.nqmhf.cn http://www.morning.hpkgm.cn.gov.cn.hpkgm.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.yrbhf.cn.gov.cn.yrbhf.cn http://www.morning.mszwg.cn.gov.cn.mszwg.cn http://www.morning.bftqc.cn.gov.cn.bftqc.cn