【RL从入门到放弃】【二十三】

博客围绕卷积神经网络展开,先介绍了定义layer时参数w、b的取值,推荐使用随机变量矩阵且不为0;接着提到用tensorboard可视化神经网络;最后阐述了卷积神经网络解决手写数据分类问题的具体结构,包括卷积层、池化层、全连接层等。

项目上做不走了,感觉遇到了难以跨越的大山,所以真正的AI落地才是难点啊!

1、定义layer

活学活用,举一反三

import tensorflow as tf
import numpy as np

x_data = np.random.rand(100).astype(np.float32)
#x_data = np.random.rand(100).astype(np.float32)
#print(x_data)
y_data = x_data*0.1+0.3
def add_layer(inputs, inputs_size, output_size, activation = None):
    w = tf.Variable(tf.random_uniform([inputs_size, output_size]))
    b = tf.Variable(tf.zeros([1])+0.1)
    if activation is None:
        y = inputs*w + b
    else:
        y = activation((inputs*w)+b)
    return w, b, y
w, b, y = add_layer(x_data, 1, 1)
#b = tf.Variable(tf.zeros([1]))
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
for step in range(100):
    sess.run(train)
    #print("loss is {0}".format(sess.run(loss)))
    if(sess.run(loss) < 0.0001):
        print("w is {0} b is {1}".format(sess.run(w), sess.run(b)))

w一般不需要是0,因为在生成初始参数时,随机变量(normal distribution)会比全部为0要好很多,所以我们这里的weights为一个in_size行, out_size列的随机变量矩阵

b一般也不是0

在机器学习中,biases的推荐值不为0,所以我们这里是在0向量的基础上又加了0.1

2、定义神经网络

import tensorflow as tf
import numpy as np

x_data = np.linspace(-1,1,300, dtype=np.float32)[:, np.newaxis]
print(x_data.shape)
noise = np.random.normal(0, 0.005, x_data.shape).astype(np.float32)
y_data = np.square(x_data)-0.5+noise
print(y_data.shape)
def add_layer(inputs, inputs_size, output_size, activation = None):
    w = tf.Variable(tf.random_uniform([inputs_size, output_size]))
    b = tf.Variable(tf.zeros([1])+0.1)
    y = tf.matmul(inputs,w )+b
    if activation is None:
        output = y
    else:
        output = activation(y)
    return output
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
l1 = add_layer(xs, 1, 10, activation=tf.nn.relu)
print(l1)
predict = add_layer(l1, 10, 1, activation=None)
print(predict)
#b = tf.Variable(tf.zeros([1]))
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - predict),reduction_indices=[1]))#这里带入的是ys
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
for step in range(100):
    sess.run(train, feed_dict={xs:x_data, ys:y_data})
    #print("loss is {0}".format(sess.run(loss)))
    #if(sess.run(loss, feed_dict={xs:x_data, ys:y_data}) < 0.116):
        #pass
        #print("w is {0} b is {1}".format(sess.run(w), sess.run(b)))

图形可视化结果:

for step in range(100):
    sess.run(train, feed_dict={xs:x_data, ys:y_data})
    if step%5==0:
        fig = plt.figure()
        ax = fig.add_subplot(1,1,1)
        print("step")
        try:
            ax.lines.remove(lines[0])
        except:
            pass
        prediction = sess.run(predict, feed_dict={xs:x_data})
        ax.scatter(x_data, y_data)
        lines = ax.plot(x_data, prediction, 'r-', lw=5)
        plt.pause(0.1)
        plt.ion()
        plt.show()

使用tensorboard

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#形成一个大的图层,将输入包起来
with tf.name_scope("input"):
    xs = tf.placeholder(np.float32, [None, 1], name="xs_in")
    ys = tf.placeholder(np.float32, [None, 1], name="ys_out")

def add_layer(inputs, in_size, out_size, activation=None):
    with tf.name_scope("layer"):
        with tf.name_scope("weight"):
            w = tf.Variable(tf.random_uniform([in_size, out_size]))
        with tf.name_scope("bias"):
            b = tf.Variable(tf.zeros([1])+0.1)
        with tf.name_scope("y"):
            y = tf.matmul(inputs, w) +b
        if activation is None:
            out_put = y
        else:
            out_put = activation(y)
        return out_put

l1 = add_layer(xs, 1, 20, activation=tf.nn.relu)
predict = add_layer(l1, 20, 1, None)

with tf.name_scope("loss"):
    loss = tf.reduce_mean(tf.square(ys-predict))
    with tf.name_scope("train"):
        optimizer = tf.train.GradientDescentOptimizer(0.1)
        train = optimizer.minimize(loss)

sess = tf.Session()
writer = tf.summary.FileWriter("logs", sess.graph)

init = tf.global_variables_initializer()
sess.run(init)
x_data = np.linspace(-1,1,300).astype(np.float32)[:, np.newaxis]
print(x_data.shape)
noise = np.random.normal(0, 1, x_data.shape).astype(np.float32)
print(noise.shape)
y_data = np.square(x_data)-0.5+noise
print(y_data.shape)

for step in range(100):
    sess.run(train, feed_dict={xs:x_data, ys:y_data})
    if step%5==0:
        print("loss is {0}".format(sess.run(loss, feed_dict={xs:x_data,ys:y_data})))

tensorboard显示过程数据

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#形成一个大的图层,将输入包起来
with tf.name_scope("input"):
    xs = tf.placeholder(np.float32, [None, 1], name="xs_in")#这里是numpy的float32的函数
    ys = tf.placeholder(np.float32, [None, 1], name="ys_out")

def add_layer(inputs, in_size, out_size, n_layer, activation=None):
    layer_name = "layer%s"%n_layer
    with tf.name_scope("layer"):
        with tf.name_scope("weight"):
            w = tf.Variable(tf.random_uniform([in_size, out_size]))
            tf.summary.histogram(layer_name+'weights', w)
        with tf.name_scope("bias"):
            b = tf.Variable(tf.zeros([1])+0.1)
            tf.summary.histogram(layer_name+'bias', b)
        with tf.name_scope("y"):
            y = tf.matmul(inputs, w) +b
        if activation is None:
            out_put = y
        else:
            out_put = activation(y)
        tf.summary.histogram(layer_name+"output", out_put)
        return out_put

l1 = add_layer(xs, 1, 20, 0, activation=tf.nn.relu)
predict = add_layer(l1, 20, 1, 1, None)

with tf.name_scope("loss"):
    loss = tf.reduce_mean(tf.square(ys-predict))
    with tf.name_scope("train"):
        optimizer = tf.train.GradientDescentOptimizer(0.1)
        train = optimizer.minimize(loss)
        tf.summary.scalar("loss", loss)
sess = tf.Session()
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("logs", sess.graph)

init = tf.global_variables_initializer()
sess.run(init)
x_data = np.linspace(-1,1,300).astype(np.float32)[:, np.newaxis]
print(x_data.shape)
noise = np.random.normal(0, 1, x_data.shape).astype(np.float32)
print(noise.shape)
y_data = np.square(x_data)-0.5+noise
print(y_data.shape)

for step in range(100):
    sess.run(train, feed_dict={xs:x_data, ys:y_data})
    if step%5==0:
        print("loss is {0}".format(sess.run(loss, feed_dict={xs:x_data,ys:y_data})))
        rs = sess.run(merged, feed_dict={xs:x_data, ys:y_data})
        writer.add_summary(rs, step)

会出来如上的board。

3、手写数据分类问题

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

with tf.name_scope("input"):
    xs = tf.placeholder(np.float32, [None, 28*28], name="in_xs")
    ys = tf.placeholder(np.float32, [None, 10], name="in_ys")

def add_layer(inputs, in_size, out_size, n_layer, activation=None):
    with tf.name_scope("layer"):
        layer_name = "layer%s"%n_layer
        with tf.name_scope("weights"):
            w = tf.Variable(tf.random_uniform([in_size, out_size]))
            tf.summary.histogram("layer"+layer_name+"weight", w)
        with tf.name_scope("bias"):
            b = tf.Variable(tf.zeros([1])+0.1)
            tf.summary.histogram("layer"+layer_name+"bias", b)
        with tf.name_scope("w_b"):
            w_b = tf.matmul(inputs, w) +b
        if activation is None:
            out_put = w_b
        else:
            out_put = activation(w_b)
        tf.summary.histogram("out_put", out_put)
        return out_put

#l1 = add_layer(xs, 28*28, 20, 0, activation=None)
predict = add_layer(xs,  28*28, 10, 1, activation=tf.nn.softmax)

with tf.name_scope("loss"):
    loss = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(predict), reduction_indices=[1]))
    tf.summary.scalar("loss", loss)
optimizer = tf.train.GradientDescentOptimizer(0.1)
with tf.name_scope("train"):
    train = optimizer.minimize(loss)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)


merge = tf.summary.merge_all()
writer = tf.summary.FileWriter("logs", sess.graph)

x_data, y_data = mnist.train.next_batch(100)


for step in range(100):
    sess.run(train, feed_dict={xs:x_data, ys:y_data})
    rs = sess.run(merge, feed_dict={xs:x_data, ys:y_data})
    if step%5==0:
        print("loss is {0}".format(sess.run(loss, feed_dict={xs:x_data, ys:y_data})))
        #print(compute_accuracy(mnist.test.images, mnist.test.labels))

4、卷积神经网络解决分类问题

  1. convolutional layer1 + max pooling;
  2. convolutional layer2 + max pooling;
  3. fully connected layer1 + dropout;
  4. fully connected layer2 to prediction.
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from tensorflow.examples.tutorials.mnist import input_data


mnist = input_data.read_data_sets('MNIST_data', one_hot=True)


def weight_variable(shape):
    inital = tf.Variable(tf.truncated_normal(shape, stddev=0.1))
    return inital
def bias_variable(shape):
    intial = tf.Variable(tf.zeros(shape)+0.1)
    return intial


"""
定义卷积,tf.nn.conv2d函数是tensoflow里面的二维的卷积函数,x是图片的所有参数,
W是此卷积层的权重,然后定义步长strides=[1,1,1,1]值,strides[0]和strides[3]的两个1是默认值,
中间两个1代表padding时在x方向运动一步,y方向运动一步,padding采用的方式是SAME。
"""
def conv2d(x, w):
    return tf.nn.conv2d(x, w, strides=[1,1,1,1], padding='SAME')


"""
接着定义池化pooling,为了得到更多的图片信息,padding时我们选的是一次一步,
也就是strides[1]=strides[2]=1,这样得到的图片尺寸没有变化,
而我们希望压缩一下图片也就是参数能少一些从而减小系统的复杂度,
因此我们采用pooling来稀疏化参数,也就是卷积神经网络中所谓的下采样层。pooling 有两种,
一种是最大值池化,一种是平均值池化,本例采用的是最大值池化tf.max_pool()。
池化的核函数大小为2x2,因此ksize=[1,2,2,1],步长为2,因此strides=[1,2,2,1]:
"""
def max_pooling(x):
    return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')

xs = tf.placeholder(tf.float32, [None, 784])/255
ys = tf.placeholder(tf.float32, [None, 10])

keep_prob = tf.placeholder(tf.float32)


x_image = tf.reshape(xs, [-1,28,28,1])

w_conv1 = weight_variable([5,5,1,32])

b_conv1 = bias_variable([32])


h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1)+b_conv1)#输入图片的厚度变厚


h_pool1 = max_pooling(h_conv1)


w_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])


h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2)+b_conv2)
h_pool2 = max_pooling(h_conv2)


h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])


w_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])

hfc1 = tf.nn.relu((tf.matmul(h_pool2_flat, w_fc1)+b_fc1))

h_fc1_drop = tf.nn.dropout(hfc1, keep_prob)

w_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
predict = tf.nn.softmax(tf.matmul(h_fc1_drop, w_fc2)+b_fc2)


cross_entrory = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(predict), reduction_indices=[1]))

optimizer = tf.train.AdamOptimizer(1e-4)
train = optimizer.minimize(cross_entrory)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)


for step in range(10):
    x_data, y_data = mnist.train.next_batch(100)
    sess.run(train, feed_dict={xs:x_data, ys:y_data, keep_prob:0.5})
    if step%5==0:
        print(sess.run(cross_entrory, feed_dict={xs:x_data, ys:y_data, keep_prob:0.5}))

 

【SCI一区复现】基于配电网韧性提升的应急移动电源预配置和动态调度(下)—MPS动态调度(Matlab代码实现)内容概要:本文档围绕“基于配电网韧性提升的应急移动电源预配置和动态调度”主题,重点介绍MPS(Mobile Power Sources)动态调度的Matlab代码实现,是SCI一区论文复现的技术资料。内容涵盖在灾害或故障等极端场景下,如何通过优化算法对应急移动电源进行科学调度,以提升配电网在突发事件中的恢复能力与供电可靠性。文档强调采用先进的智能优化算法进行建模求解,并结合IEEE标准测试系统(如IEEE33节点)进行仿真验证,具有较强的学术前沿性和工程应用价值。; 适合人群:具备电力系统基础知识和Matlab编程能力,从事电力系统优化、配电网韧性、应急电源调度等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于复现高水平期刊(SCI一区、IEEE顶刊)中关于配电网韧性与移动电源调度的研究成果;②支撑科研项目中的模型构建与算法开发,提升配电网在故障后的快速恢复能力;③为电力系统应急调度策略提供仿真工具与技术参考。; 阅读建议:建议结合前篇“MPS预配置”内容系统学习,重点关注动态调度模型的数学建模、目标函数设计与Matlab代码实现细节,建议配合YALMIP等优化工具包进行仿真实验,并参考文中提供的网盘资源获取完整代码与数据。
一款AI短视频生成工具,只需输入一句产品卖点或内容主题,软件便能自动生成脚本、配音、字幕和特效,并在30秒内渲染出成片。 支持批量自动剪辑,能够实现无人值守的循环生产。 一键生成产品营销与泛内容短视频,AI批量自动剪辑,高颜值跨平台桌面端工具。 AI视频生成工具是一个桌面端应用,旨在通过AI技术简化短视频的制作流程。用户可以通过简单的提示词文本+视频分镜素材,快速且自动的剪辑出高质量的产品营销和泛内容短视频。该项目集成了AI驱动的文案生成、语音合成、视频剪辑、字幕特效等功能,旨在为用户提供开箱即用的短视频制作体验。 核心功能 AI驱动:集成了最新的AI技术,提升视频制作效率和质量 文案生成:基于提示词生成高质量的短视频文案 自动剪辑:支持多种视频格式,自动化批量处理视频剪辑任务 语音合成:将生成的文案转换为自然流畅的语音 字幕特效:自动添加字幕和特效,提升视频质量 批量处理:支持批量任务,按预设自动持续合成视频 多语言支持:支持中文、英文等多种语言,满足不同用户需求 开箱即用:无需复杂配置,用户可以快速上手 持续更新:定期发布新版本,修复bug并添加新功能 安全可靠:完全本地本地化运行,确保用户数据安全 用户友好:简洁直观的用户界面,易于操作 多平台支持:支持Windows、macOS和Linux等多个操作系统
源码来自:https://pan.quark.cn/s/2bb27108fef8 **MetaTrader 5的智能交易系统(EA)**MetaTrader 5(MT5)是由MetaQuotes Software Corp公司研发的一款广受欢迎的外汇交易及金融市场分析软件。 该平台具备高级图表、技术分析工具、自动化交易(借助EA,即Expert Advisor)以及算法交易等多项功能,使交易参与者能够高效且智能化地开展市场活动。 **抛物线SAR(Parabolic SAR)技术指标**抛物线SAR(Stop and Reverse)是由技术分析专家Wells Wilder所设计的一种趋势追踪工具,其目的在于识别价格走势的变动并设定止损及止盈界限。 SAR值的计算依赖于当前价格与前一个周期的SAR数值,随着价格的上扬或下滑,SAR会以一定的加速系数逐渐靠近价格轨迹,一旦价格走势发生逆转,SAR也会迅速调整方向,从而发出交易提示。 **Parabolic SAR EA的操作原理**在MetaTrader 5环境中,Parabolic SAR EA借助内嵌的iSAR工具来执行交易决策。 iSAR工具通过计算得出的SAR位置,辅助EA判断入市与离市时机。 当市场价位触及SAR点时,EA将产生开仓指令,倘若价格持续朝同一方向变动,SAR将同步移动,形成动态止损与止盈参考点。 当价格反向突破SAR时,EA会结束当前仓位并可能建立反向仓位。 **智能交易系统(EA)的优越性**1. **自动化交易**:EA能够持续监控市场,依据既定策略自动完成买卖操作,减少人为情感对交易的影响。 2. **精确操作**:EA依照预设规则操作,无任何迟疑,从而提升交易成效。 3. **风险管控**:借助SA...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值