机器学习,入门---------一段简单的可以运行的机器学习代码

使用tensorflow的一段机器运行的代码,配好python环境,加上tensorflow就可以运行,先不解释这段代码了,相信读者可以看懂,代码来自于莫烦大神,推荐一下大神的课哈

https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/5-08-RNN2/

import tensorflow as tf
import numpy as np

#creat data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3

### create tensorflow struct start ###

Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0))
biases = tf.Variable(tf.zeros([1]))
y = Weights*x_data + biases

loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

init = tf.initialize_all_variables()
### create tensorflow structure end ###

sess = tf.Session()
sess.run(init)   #very important

for step in range(201):
    sess.run(train)
    if step % 20 == 0:
        print(step,sess.run(Weights),sess.run(biases))

 

你可能感兴趣的:(机器学习)