zoukankan      html  css  js  c++  java
  • mac下安装tensorflow及入门例子

    https://www.tensorflow.org/install/install_mac 

    使用virtualenv安装,virtualenv相当于使tensorflow运行在虚拟机环境下。

    需要使用source ~/tensorflow/bin/activate 命令,(我安装在了

    source /Users/work/builded/tensorflow/bin/activate,bash下/是硬盘根目录,~/是用户根目录)

    命令行变成 (tensorflow)$ 这样,才可以使用 import tensorflow as tf这些

    使用ptython运行一个简单的y = -x +1训练模型:

    (tensorflow) $ python tftrain.py

    import numpy as np
    import tensorflow as tf
    
    # Model parameters
    W = tf.Variable([.3], tf.float32)
    b = tf.Variable([-.3], tf.float32)
    # Model input and output
    x = tf.placeholder(tf.float32)
    linear_model = W * x + b
    y = tf.placeholder(tf.float32)
    # loss
    loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
    # optimizer
    optimizer = tf.train.GradientDescentOptimizer(0.01)
    train = optimizer.minimize(loss)
    # training data
    x_train = [1,2,3,4]
    y_train = [0,-1,-2,-3]
    # training loop
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init) # reset values to wrong
    for i in range(1000):
      sess.run(train, {x:x_train, y:y_train})
    
    # evaluate training accuracy
    curr_W, curr_b, curr_loss  = sess.run([W, b, loss], {x:x_train, y:y_train})
    print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))
  • 相关阅读:
    mysql表结构同步
    关于Java8中lambda约简函数reduce的一个计算问题
    激烈的歌曲有助于编程
    今天刷了数据解构与算法这门课 感觉略有收获
    我有一个好朋友 他的名字叫刘洋 他的ID是北极的大企鹅 他的技术不错 他渴望成为架构师 猎头们路过可以去他的博客看看
    缓存雪崩,缓存击穿,缓存穿透
    celery
    Redis
    django 缓存的使用
    base64 加密
  • 原文地址:https://www.cnblogs.com/mlj318/p/6439997.html
Copyright © 2011-2022 走看看