Tensorflow-note-线性回归

Tensorflow实现线性回归

问题:怎么使用tensorflow实现简单的线性回归?

解答:

1.环境准备:

Dockerfile构建tensorflow的环境
python 3

2.实践代码

from __future__ import print_function
import tensorflow as tf
import numpy as np
#创建100个32位浮点数的数据
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3

#创建所需线性回归的结构
##权重随机数(-1.0,1.0)
Weights = tf.Variable(tf.random_uniform[1],-1.0,1.0))
##偏置数/纵轴,(0)
biases = tf.Variable(tf.zeros[1])
##线性回归函数(权重×随机数+偏置)
y = Weights*x_data + biases
##方差公式
loss = tf.reduce_mean(tf.square(y-y_data))
##优化器(还有什么优化器呢?),这里是随机下降优化器,每次下降0.5
optimizer = tf.train.GradientDescentOptimizer(0.5)
#启动程序流程
##开一个会话,(tensorflow最近出了一个不使用的用法,后续会讲)
sess = tf.Session
##检测tensorflow版本
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
    init = tf.initialize_all_Variables()
else:
    init = tf.global_variables_initializer()
sess.run(init)

for step in range(201):    
    sess.run(train)
    #20次输出一次日志
    if step % 20 == 0:
        print(step, sess.run(Weights), sess.run(biases))

特别感谢莫烦大神的教程!

参考

莫烦Youku video tutorial: http://i.youku.com/pythontutorial

### TensorFlow GPU Version 1.13 Installation and Configuration For installing TensorFlow GPU version 1.13, it is important to ensure compatibility with other components such as CUDA and cuDNN versions. The process involves setting up a suitable Python environment using Conda or virtualenv, ensuring the correct drivers are installed on the system, and then proceeding with the installation of TensorFlow. #### Setting Up Environment Using Conda To set up an isolated environment specifically for TensorFlow-GPU 1.13: ```bash conda create -n tensorflow_gpu python=3.6 anaconda conda activate tensorflow_gpu ``` Once inside this new environment, proceed by installing `ipykernel` which allows Jupyter notebooks to recognize this specific conda environment[^1]: ```bash conda install --prefix=/path/to/your/env ipykernel ``` Replace `/path/to/your/env` with the actual path where the environment resides. #### Installing Required Drivers Before installing TensorFlow, verify that the appropriate NVIDIA drivers have been installed correctly. During setup prompts similar to those encountered during driver installations may appear asking about running Nvidia-xconfig utilities; choose according to personal preference but note selecting yes will modify X configuration files[^4]. Ensure having compatible versions of CUDA (9.0) and cuDNN (7.4). These can typically be obtained from the official NVIDIA website based on hardware requirements. #### Installing TensorFlow-GPU 1.13 via Pip With all prerequisites met, now comes time to actually install TensorFlow-GPU 1.13 through pip within the activated conda environment: ```bash pip install tensorflow-gpu==1.13.1 ``` This command installs exactly version 1.13.1 of TensorFlow optimized for GPUs into the current active environment. After completing these steps, one should test whether everything has been configured properly by launching Python interpreter or IPython/Jupyter Notebook session under said environment and importing TensorFlow without encountering errors related to missing libraries or incompatible configurations.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值