吴裕雄 python 神经网络——TensorFlow 自定义损失函数
import tensorflow as tf
from numpy.random import RandomState batch_size = 8
x = tf.placeholder(tf.float32, shape=(None, 2), name="x-input")
y_ = tf.placeholder(tf.float32, shape=(None, 1), name='y-input')
w1= tf.Variable(tf.random_normal([2, 1], stddev=1, seed=1))
y = tf.matmul(x, w1) # 定义损失函数使得预测少了的损失大,于是模型应该偏向多的方向预测。
loss_less = 10
loss_more = 1
loss = tf.reduce_sum(tf.where(tf.greater(y, y_), (y - y_) * loss_more, (y_ - y) * loss_less))
train_step = tf.train.AdamOptimizer(0.001).minimize(loss) rdm = RandomState(1)
X = rdm.rand(128,2)
Y = [[x1+x2+(rdm.rand()/10.0-0.05)] for (x1, x2) in X] with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
STEPS = 5000
for i in range(STEPS):
start = (i*batch_size) % 128
end = (i*batch_size) % 128 + batch_size
sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})
if i % 1000 == 0:
print("After %d training step(s), w1 is: " % (i))
print sess.run(w1), "\n"
print "Final w1 is: \n", sess.run(w1)

loss_less = 1
loss_more = 10
loss = tf.reduce_sum(tf.where(tf.greater(y, y_), (y - y_) * loss_more, (y_ - y) * loss_less))
train_step = tf.train.AdamOptimizer(0.001).minimize(loss) with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
STEPS = 5000
for i in range(STEPS):
start = (i*batch_size) % 128
end = (i*batch_size) % 128 + batch_size
sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})
if i % 1000 == 0:
print("After %d training step(s), w1 is: " % (i))
print sess.run(w1), "\n"
print "Final w1 is: \n", sess.run(w1)

loss = tf.losses.mean_squared_error(y, y_)
train_step = tf.train.AdamOptimizer(0.001).minimize(loss) with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
STEPS = 5000
for i in range(STEPS):
start = (i*batch_size) % 128
end = (i*batch_size) % 128 + batch_size
sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})
if i % 1000 == 0:
print("After %d training step(s), w1 is: " % (i))
print sess.run(w1), "\n"
print "Final w1 is: \n", sess.run(w1)

吴裕雄 python 神经网络——TensorFlow 自定义损失函数的更多相关文章
- 吴裕雄 python 神经网络——TensorFlow 循环神经网络处理MNIST手写数字数据集
#加载TF并导入数据集 import tensorflow as tf from tensorflow.contrib import rnn from tensorflow.examples.tuto ...
- 吴裕雄 python 神经网络——TensorFlow 使用卷积神经网络训练和预测MNIST手写数据集
import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_dat ...
- 吴裕雄 python 神经网络——TensorFlow 训练过程的可视化 TensorBoard的应用
#训练过程的可视化 ,TensorBoard的应用 #导入模块并下载数据集 import tensorflow as tf from tensorflow.examples.tutorials.mni ...
- 吴裕雄 python 神经网络TensorFlow实现LeNet模型处理手写数字识别MNIST数据集
import tensorflow as tf tf.reset_default_graph() # 配置神经网络的参数 INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE ...
- 吴裕雄 python 神经网络——TensorFlow 数据集高层操作
import tempfile import tensorflow as tf train_files = tf.train.match_filenames_once("E:\\output ...
- 吴裕雄 python 神经网络——TensorFlow 输入数据处理框架
import tensorflow as tf files = tf.train.match_filenames_once("E:\\MNIST_data\\output.tfrecords ...
- 吴裕雄 python 神经网络——TensorFlow 花瓣分类与迁移学习(2)
import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...
- 吴裕雄 python 神经网络——TensorFlow 花瓣识别2
import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...
- 吴裕雄 python 神经网络——TensorFlow训练神经网络:不使用滑动平均
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...
随机推荐
- [linux] Ubuntu18.04 安装mysql密码不对
安装 安装过程可以说是非常简单了 sudo apt-get install mysql-server 然后看看有没有启动成功 systemctl status mysql 看到状态是 running就 ...
- AOP使用
package com.googosoft.db.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lan ...
- nginx-vue项目在window下的部署
主机->操作系统->window(.net)/Linux->tomcat/nginx(软件反向代理) 1.vue项目打包到dist文件夹(之后把dist文件夹放到服务器就可以了) n ...
- python的os库
os库(operating system,提供操作系统函数) 1. __file__是什么? ans:当前文件的名字. 例如r.py内容如下 import os if __name__ == &quo ...
- 转载:Bass management
https://kenrockwell.com/audio/bass-management.htm https://www.axiomaudio.com/blog/bassmanagement htt ...
- leetcode78.子集➕90.子集2
78子集 dfs dfs1: 和全排列的区别就是对于当前考察的索引i,全排列如果不取i,之后还要取i,所以需要一个visited数组用来记录.对于子集问题如果不取i,之后也不必再取i. 单纯递归回溯 ...
- php提供下载服务实例
两个步骤:1,通过header头信息告诉浏览器,我给你回应的是一个附件请接收 2,通过php读取下载的文件的内容并返回 前端 <!DOCTYPE html> <html lang=& ...
- Winform 随机抽奖小程序
效果图: 主要代码: Form1.cs using System; using System.Drawing; using System.IO; using System.Runtime.Intero ...
- 【音乐欣赏】《Siren》 - The Chainsmokers / Aazar
曲名:Siren 作者:The Chainsmokers . Aazar [00:00.00] 作曲 : Alex Pall/Andrew Taggart/Alexis Duvivier [00:01 ...
- Linux 下使用 ffmpeg 大批量合并 ts 文件, mp4切割文件为m3u8
见范例 ffmpeg -i "concat:file001.ts|file002.ts|file003.ts|file004.ts......n.ts" -acodec copy ...