zoukankan      html  css  js  c++  java
  • tensorflow

    开始
    Source activate tensorflow
    python
    import tensorflow as tf

    定义
    node1 = tf.constant(3.0, dtype=tf.float32)
    node2 = tf.constant(4.0) # also tf.float32 implicitly
    print(node1, node2)
    打印

    sess = tf.Session()
    print(sess.run([node1, node2]))
    加法
    node3 = tf.add(node1, node2)
    print("node3:", node3)
    print("sess.run(node3):", sess.run(node3))


    占位符加法
    定义部分
    a = tf.placeholder(tf.float32)
    b = tf.placeholder(tf.float32)
    adder_node = a + b  # + provides a shortcut for tf.add(a, b)
    运算部分

    print(sess.run(adder_node, {a: 3, b: 4.5}))
    print(sess.run(adder_node, {a: [1, 3], b: [2, 4]}))

    占位符符合操作
    add_and_triple = adder_node * 3.
    print(sess.run(add_and_triple, {a: 3, b: 4.5}))


    模型

    定义
    W = tf.Variable([.3], dtype=tf.float32)
    b = tf.Variable([-.3], dtype=tf.float32)
    x = tf.placeholder(tf.float32)
    linear_model = W * x + b
    初始化
    init = tf.global_variables_initializer()
    sess.run(init)
    运算
    print(sess.run(linear_model, {x: [1, 2, 3, 4]}))


    损失函数
    y = tf.placeholder(tf.float32)
    squared_deltas = tf.square(linear_model - y)
    loss = tf.reduce_sum(squared_deltas)
    print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))

    调整线性回归的参数
    fixW = tf.assign(W, [-1.])
    fixb = tf.assign(b, [1.])
    sess.run([fixW, fixb])
    print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))

    梯度下降优化器
    optimizer = tf.train.GradientDescentOptimizer(0.01)
    train = optimizer.minimize(loss)

    sess.run(init) # reset values to incorrect defaults.
    for i in range(1000):
      sess.run(train, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]})
    print(sess.run([W, b]))


    完整线性回归
    import tensorflow as tf
    # Model parameters
    W = tf.Variable([.3], dtype=tf.float32)
    b = tf.Variable([-.3], dtype=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))


    tf.estimator

  • 相关阅读:
    jQuery序列化
    jQuery的ajax与django传参
    Django中的cookie与session操作
    Django文件上传
    Django表单的简单应用
    django加载模板文件
    django-admin.py创建项目失败解决方法
    django笔记
    unity创建xml与加载xml
    JavaScript相关
  • 原文地址:https://www.cnblogs.com/xiaoyingying/p/7661246.html
Copyright © 2011-2022 走看看