TensorFlow-Examples - basic_operations.py

TensorFlow-Examples - basic_operations.py

https://github.com/aymericdamien/TensorFlow-Examples

1. basic_operations.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

'''
Basic Operations example using TensorFlow library.
Project: https://github.com/aymericdamien/TensorFlow-Examples/
'''

import tensorflow as tf

# Basic constant operations
# The value returned by the constructor represents the output of the Constant op.
a = tf.constant(2)
b = tf.constant(3)

# Launch the default graph.
with tf.Session() as sess:
    print("a=2, b=3")
    print("Addition with constants: %i" % sess.run(a + b))
    print("Multiplication with constants: %i" % sess.run(a * b))
/usr/bin/python2.7 /home/strong/git_workspace/MonoGRNet/yongqiang.py
2019-07-19 13:50:15.712647: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-07-19 13:50:15.802164: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-07-19 13:50:15.802400: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties: 
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.51GiB
2019-07-19 13:50:15.802410: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
a=2, b=3
Addition with constants: 5
Multiplication with constants: 6

Process finished with exit code 0

2. basic_operations.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

'''
Basic Operations example using TensorFlow library.
Project: https://github.com/aymericdamien/TensorFlow-Examples/
'''

import tensorflow as tf

# Basic Operations with variable as graph input
# The value returned by the constructor represents the output of the Variable op. (define as input when running session)

# tf Graph input
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)

# Define some operations
add = tf.add(a, b)
mul = tf.multiply(a, b)

# Launch the default graph.
with tf.Session() as sess:
    # Run every operation with variable input
    print("Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3}))
    print("Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3}))
/usr/bin/python2.7 /home/strong/git_workspace/MonoGRNet/yongqiang.py
2019-07-19 13:55:43.941931: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-07-19 13:55:44.036639: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-07-19 13:55:44.036967: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties: 
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.50GiB
2019-07-19 13:55:44.036982: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
Addition with variables: 5
Multiplication with variables: 6

Process finished with exit code 0

3. basic_operations.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

'''
Basic Operations example using TensorFlow library.
Project: https://github.com/aymericdamien/TensorFlow-Examples/
'''

import tensorflow as tf

# Matrix Multiplication from TensorFlow official tutorial

# Create a Constant op that produces a 1x2 matrix. The op is added as a node to the default graph.
# The value returned by the constructor represents the output of the Constant op.
matrix1 = tf.constant([[3., 3.]])

# Create another Constant that produces a 2x1 matrix.
matrix2 = tf.constant([[2.], [2.]])

# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.
# The returned value, 'product', represents the result of the matrix multiplication.
product = tf.matmul(matrix1, matrix2)

# To run the matmul op we call the session 'run()' method, passing 'product' which represents the output of the matmul op.
# This indicates to the call that we want to get the output of the matmul op back.
#
# All inputs needed by the op are run automatically by the session. They typically are run in parallel.
#
# The call 'run(product)' thus causes the execution of threes ops in the graph: the two constants and matmul.
# The output of the op is returned in 'result' as a numpy `ndarray` object.
# ==> [[ 12.]]

with tf.Session() as sess:
    result = sess.run(product)
    print(result)
/usr/bin/python2.7 /home/strong/git_workspace/MonoGRNet/yongqiang.py
2019-07-19 14:03:10.717588: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-07-19 14:03:10.809048: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-07-19 14:03:10.809299: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties: 
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.50GiB
2019-07-19 14:03:10.809310: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
[[12.]]

Process finished with exit code 0
上一篇:杭电多校第一场-B-Operation


下一篇:Reshapeing operations