- Practical Convolutional Neural Networks
- Mohit Sewak Md. Rezaul Karim Pradeep Pujari
- 337字
- 2021-06-24 18:58:50
Basic math with TensorFlow
The tf.add() function takes two numbers, two tensors, or one of each, and it returns their sum as a tensor:
Addition
x = tf.add(1, 2, name=None) # 3
Here's an example with subtraction and multiplication:
x = tf.subtract(1, 2,name=None) # -1
y = tf.multiply(2, 5,name=None) # 10
What if we want to use a non-constant? How to feed an input dataset to TensorFlow? For this, TensorFlow provides an API, tf.placeholder(), and uses feed_dict.
A placeholder is a variable that data is assigned to later in the tf.session.run() function. With the help of this, our operations can be created and we can build our computational graph without needing the data. Afterwards, this data is fed into the graph through these placeholders with the help of the feed_dict parameter in tf.session.run() to set the placeholder tensor. In the following example, the tensor x is set to the string Hello World before the session runs:
x = tf.placeholder(tf.string)
with tf.Session() as sess:
output = sess.run(x, feed_dict={x: 'Hello World'})
It's also possible to set more than one tensor using feed_dict, as follows:
x = tf.placeholder(tf.string)
y = tf.placeholder(tf.int32, None)
z = tf.placeholder(tf.float32, None)
with tf.Session() as sess:
output = sess.run(x, feed_dict={x: 'Welcome to CNN', y: 123, z: 123.45})
Placeholders can also allow storage of arrays with the help of multiple dimensions. Please see the following example:
import tensorflow as tf
x = tf.placeholder("float", [None, 3])
y = x * 2
with tf.Session() as session:
input_data = [[1, 2, 3],
[4, 5, 6],]
result = session.run(y, feed_dict={x: input_data})
print(result)
The tf.truncated_normal() function returns a tensor with random values from a normal distribution. This is mostly used for weight initialization in a network:
n_features = 5
n_labels = 2
weights = tf.truncated_normal((n_features, n_labels))
with tf.Session() as sess:
print(sess.run(weights))
- 劍破冰山:Oracle開發藝術
- 分布式數據庫系統:大數據時代新型數據庫技術(第3版)
- INSTANT Cytoscape Complex Network Analysis How-to
- The Game Jam Survival Guide
- 數據庫技術及應用教程
- 深入淺出 Hyperscan:高性能正則表達式算法原理與設計
- 重復數據刪除技術:面向大數據管理的縮減技術
- Oracle數據庫管理、開發與實踐
- 數字IC設計入門(微課視頻版)
- 數據修復技術與典型實例實戰詳解(第2版)
- Access數據庫開發從入門到精通
- Filecoin原理與實現
- MySQL技術內幕:InnoDB存儲引擎
- SQL Server 2008寶典(第2版)
- 數據庫原理與設計實驗教程(MySQL版)