李宏毅 Tensorflow解决Fizz Buzz问题
提出问题
一个网友的博客,记录他在一次面试时,碰到面试官要求他在白板上用TensorFlow写一个简单的网络实现异或(XOR)功能。这个本身并不难,单层感知器不能解决异或问题是学习神经网络中的一个常识,而简单的两层神经网络却能将其轻易解决。但这个问题的难处在于,我们接触TensorFlow通常直接拿来写CNN,或者其他的深度学习相关的网络了,而实现这种简单网络,基本上从未做过;更何况,要求在白板上写出来,如果想bug free,并不是容易的事儿啊。
数据
李宏毅老师对数据进行了如下分析
对数字101到1000做了labeling,即训练数据xtrain.shape=(900,10),每一个数字都是用二进位来表示,第一个数字是101,用二进位来表示即为[1,0,1,0,0,1,1,0,0,0],每一位表示2^{n-1},n表示左数第几位。现在一共有四个case,[一般,Fizz,Buzz,Fizz Buzz],所以y_train.shape=(900,10),对应的维度用1表示,其他都为0
数据分析
900个样本,每个样本代表一个数字,并且有二进制值数值表示。观察样本,第一个数字是101,则二进制的表示为1010011000
然后观察label
如果不是被3,5或者同时被整出,则保留原数。所以一共有四种情况。“原数,fizz,buzz,fizz&buzz”
inputlayer是10维度,为中间隐藏层输出100维,最终输出4维。激活函数用的是ReLU函数,compile配置中,是做分类,学习率优化函数是adam,再设置batchsize和epoch即可。
源代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/9/13 11:12
# @Author : BaoBao
# @Mail : baobaotql@163.com
# @File : test6.py.py
# @Software: PyCharm #fizz buzz问题 from keras.layers.normalization import BatchNormalization
from keras.models import Sequential
from keras.layers.core import Dense,Dropout,Activation
from keras.optimizers import SGD,Adam
import numpy as np def fizzbuzz(start,end):
x_train,y_train=[],[]
for i in range(start,end+1):
num = i
tmp=[0]*10
j=0
while num :
tmp[j] = num & 1
num = num>>1
j+=1
x_train.append(tmp)
if i % 3 == 0 and i % 5 ==0:
y_train.append([0,0,0,1])
elif i % 3 == 0:
y_train.append([0,1,0,0])
elif i % 5 == 0:
y_train.append([0,0,1,0])
else :
y_train.append([1,0,0,0])
return np.array(x_train),np.array(y_train) x_train,y_train = fizzbuzz(101,1000) #打标记函数
x_test,y_test = fizzbuzz(1,100) model = Sequential()
model.add(Dense(input_dim=10,output_dim=10))
model.add(Activation('relu'))
model.add(Dense(output_dim=4))
model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy']) model.fit(x_train,y_train,batch_size=20,nb_epoch=100) result = model.evaluate(x_test,y_test,batch_size=1000) print('Acc:',result[1])
运行结果
可以看到accuray的结果不是很好,那么我们考虑多种方案来解决,比如修改Activation function,或者将hidden neure修改为1000后 可以看到accuracy的明显提高
运行结果
----------------------------------华丽的分割线------------------------------------------
从别的博主看到的一个方法写的很简单QAQ我好菜啊
1.import 模块
import numpy as np
import tensorflow as tf
2.输入数据的placeholder
具体来说,一个data,一个label
data = tf.placeholder(tf.float32, shape=(4, 2))
label = tf.placeholder(tf.float32, shape=(4, 1))
由于本例比较特殊,异或只有四种输入和对应的四个输出,所以根据需求定义固定的shape
3.基于输入数据的placeholder构建model
异或需要两层神经网络,每层分别需要一个weights和一个bias,所以定义如下:
with tf.variable_scope('layer1') as scope:
weight = tf.get_variable(name='weight', shape=(2, 2))
bias = tf.get_variable(name='bias', shape=(2,))
x = tf.nn.sigmoid(tf.matmul(data, weight) + bias)
with tf.variable_scope('layer2') as scope:
weight = tf.get_variable(name='weight', shape=(2, 1))
bias = tf.get_variable(name='bias', shape=(1,))
x = tf.matmul(x, weight) + bias
这里为了方便变量管理,以及在tensorboard上的条理性,使用了variable_scope,当然,也可以不使用。
此外,因为我们后面的loss要使用sigmoid_cross_entropy_with_logits函数,所以这里第二层网络的输出没有过sigmoid函数。如果loss使用其他函数,则可以做相应处理。
4.定义loss
其实这里可以灵活选用各种loss函数,比如MSE,等等。但我们还是选用了在CNN中广泛使用的cross entropy
preds = tf.nn.sigmoid(x)
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=label, logits=x))
我们这里把模型的输出x过了一下sigmoid函数,作为最终输出,以便在训练时对模型进行监视。
5.定义Optimizer
learning_rate = tf.placeholder(tf.float32)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
6.构建并输入数据,开始训练
train_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
train_label = np.array([[0], [1], [1], [0]])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(10000):
if step < 3000:
lr = 1
elif step < 6000:
lr = 0.1
else:
lr = 0.01
_, l, pred = sess.run([optimizer, loss, preds], feed_dict={data: train_data, label: train_label, learning_rate: lr})
if step % 500:
print('Step: {} -> Loss: {} -> Predictions: {}'.format(step, l, pred)
完整代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/9/13 11:24
# @Author : BaoBao
# @Mail : baobaotql@163.com
# @File : test7.py
# @Software: PyCharm #fizz buzz问题
import numpy as np
import tensorflow as tf data = tf.placeholder(tf.float32, shape=(4, 2))
label = tf.placeholder(tf.float32, shape=(4, 1)) with tf.variable_scope('layer1') as scope:
weight = tf.get_variable(name='weight', shape=(2, 2))
bias = tf.get_variable(name='bias', shape=(2,))
x = tf.nn.sigmoid(tf.matmul(data, weight) + bias) with tf.variable_scope('layer2') as scope:
weight = tf.get_variable(name='weight', shape=(2, 1))
bias = tf.get_variable(name='bias', shape=(1,))
x = tf.matmul(x, weight) + bias preds = tf.nn.sigmoid(x) loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=label, logits=x)) learning_rate = tf.placeholder(tf.float32) optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) train_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) train_label = np.array([[0], [1], [1], [0]]) with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(10000):
if step < 3000:
lr = 1
elif step < 6000:
lr = 0.1
else:
lr = 0.01
_, l, pred = sess.run([optimizer, loss, preds], feed_dict={data: train_data, label: train_label, learning_rate: lr})
if step % 500:
print('Step: {} -> Loss: {} -> Predictions: {}'.format(step, l, pred))
运行结果
李宏毅 Tensorflow解决Fizz Buzz问题的更多相关文章
- 【笔记】机器学习 - 李宏毅 - 11 - Keras Demo2 & Fizz Buzz
1. Keras Demo2 前节的Keras Demo代码: import numpy as np from keras.models import Sequential from keras.la ...
- [LeetCode] Fizz Buzz 嘶嘶嗡嗡
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- Lintcode 9.Fizz Buzz 问题
------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of s ...
- LeetCode 412. Fizz Buzz
Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...
- LeetCode Fizz Buzz
原题链接在这里:https://leetcode.com/problems/fizz-buzz/ 题目: Write a program that outputs the string represe ...
- Fizz Buzz
class Solution { public: /** * param n: As description. * return: A list of strings. */ vector<st ...
- LintCode (9)Fizz Buzz
下面是AC代码,C++风格: class Solution { public: vector<string> fizzBuzz(int N) { vector<string> ...
- [重构到模式-Chain of Responsibility Pattern]把Fizz Buzz招式重构到责任链模式
写一段程序从1打印到100,但是遇到3的倍数时打印Fizz,遇到5的倍数时打印Buzz,遇到即是3的倍数同时也是5的倍数时打印FizzBuzz.例如: 1 2 Fizz 4 Buzz Fizz 7 8 ...
- Swift完成fizz buzz test
看到一篇文章上说,很多貌似看过很多本编程书的童鞋连简单的fizz buzz测试都完不成. 不知道fizz buzz test为何物的,建议自行搜之. 测试要求是,编写满足以下条件的代码: Write ...
随机推荐
- Java实现一行一行读取文件内容(进行编码处理)
// 读取文件内容public String readFile(){ String path = ""; File file = new File(path); StringBui ...
- 「BZOJ 5161」最长上升子序列「状压DP」
题意 求一个\(1\sim n\)的排列LIS的期望长度,\(n\leq 28\) 题解 考虑朴素的LIS:\(f[i] = min(f[j]) + 1\) 记\(mx[i]\)为\(f\)的前缀最大 ...
- POJ 3177 (Redundant Paths) —— (有重边,边双联通,无向图缩点)
做到这里以后,总算是觉得tarjan算法已经有点入门了. 这题的题意是,给出若干个点和若干条边连接他们,在这个无向图中,问至少增加多少条边可以使得这个图变成边双联通图(即任意两点间都有至少两条没有重复 ...
- 微信支付宝xposed个人收款免签支付源码
源码介绍: 个人免签支付是指使用自己的微信支付宝账号作为个人网站的收款账号,网站订单支付成功后,网站能实时收到成功回调信息. 系统基于xposed逆向微信.支付宝.云闪付来实现个人收款免 ...
- 预处理、const、static与sizeof-使用const与#define的特点及区别
1:#define只是用来做文本替换的.例如: #define PI 3.1415926 float angle; angle=*PI/; 那么,程序进行编译的时候,编译器会首先将“#define P ...
- OUC_Summer Training_ DIV2_#7 718
是18号做的题啦,现在才把报告补上是以前不重视报告的原因吧,不过现在真的很喜欢写报告,也希望能写一些有意义的东西出来. A - Dragons Time Limit:2000MS Memory ...
- Linux字符编码默认为UTF-8,如出现乱码可设置为GBK
Linux字符编码默认为UTF-8,如出现乱码可设置为GBK1.手动更改profile文件的命令: vi /etc/profile 也可以修改 /etc/sysconfig/i18n 文件,如 LAN ...
- adb、pm命令操作apk包
1.adb shell pm list package 打印出来所有安装到手机上的APP包名 2.adb shell pm path com.xxx.xxx 找出安装后的包名应用的apk所在位置 3. ...
- 数据库 | SQL查询&LIMIT的用法
body{ text-align:left; width:80%; margin:10px 100px; } 前言 select top n 形式的语句可以获取查询的前几个记录,但是 mysql没有此 ...
- Repeater, DataList, 和GridView的区别及使用
从对象层次图可以看出,Repeater是最轻最小的控件,它仅仅继承了基本控件的功能,包括ID属性.子控件集合等.另一方面,DataList和DataGrid则继承了WebControl功能,包括样式和 ...