tf.graph constructs a graph to execute computation. Node: tf.Operation; edge: tf.Tensor.
tf.name_scope : add a prefix to all operations created in this scope.
tf.constant(42.0): create a tf.Operation hat produces the value 42.0, and returns a tf.Tensor that prepresents the value of the constant.
tf.matmul(x,y): multiplies x and y, and returns a tensor that represents the result of the multiplication.
Conv1D(input, filters, kernel_size, activation)
Questions: The input shape should be (batch_size, length, channel). However, for a time series sequence, what does this channel mean? In image field, the channel means RGB, or grey image.
Answer: in one time series sequence, the channel is 1. Channel means the amount of attributes(time series) you have, like price, power consumption, outside temperature and so on. From Multi-Channel Convolutions explained with… MS Excel!
MNIST example
image.shape(60000, 28, 28). length = 60000;
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
The first layer in this network, tf.keras.layers.Flatten, transforms the format of the images from a two-dimensional array (of 28 by 28 pixels) to a one-dimensional array (of 28 * 28 = 784 pixels). This layer has no parameters to learn; it only reformats the data.
After the pixels are flattened, the network consists of a sequence of two tf.keras.layers.Dense layers. These are densely connected, or fully connected, neural layers. The first Dense layer has 128 nodes (or neurons). The second (and last) layer is a 10-node softmax layer that returns an array of 10 probability scores that sum to 1. Each node contains a score that indicates the probability that the current image belongs to one of the 10 classes.
The difference between dense layers and sparse layers. Dense layers are fully connected, Sparse layers aren’t, like drop out operation.
Program Bug:
feed = {inputs_: x, labels_: y, keep_prob_: 0.5, learning_rate_:
learning_rate}
# Loss
loss, _, acc = sess.run([cost, optimizer, accuracy], feed_dict=feed)
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles. For reference, the tensor object was Tensor(“Reshape_1:0”, shape=(100, 24, 1), dtype=float32) which was passed to the feed with key Tensor(“inputs:0”, shape=(?, 24, 1), dtype=float32).
How to solve this:
with tf.compat.v1.Session() as sess:
sess.run(...)
the result of sess.run() is numpy ndarray rather than tensor. In this way, you can turn a tensor into a ndarray, but this isn’t a good way. You have to open multiple session and then close it.
What is a tensor: Tensors
TensorFlow, as the name indicates, is a framework to define and run computations involving tensors. A tensor is a generalization of vectors and matrices to potentially higher dimensions. Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes.
A tf.Tensorobject represents a partially defined computation that will eventually produce a value. TensorFlow programs work by first building a graph of tf.Tensor objects, detailing how each tensor is computed based on the other available tensors and then by running parts of this graph to achieve the desired results.
The difference between scalar and vector: 标量(Scalar),又称纯量,是只有大小,没有方向,可用实数表示的一个量,实际上标量就是实数,标量这个称法只是为了区别与向量的差别。
The rank of a tf.Tensor object is its number of dimensions.
| Rank |
Math entity |
| 0 |
Scalar (magnitude大小 only) |
| 1 |
Vector (magnitude and direction) |
| 2 |
Matrix (table of numbers) |
| 3 |
3-Tensor (cube of numbers) |
| n |
n-Tensor (you get the idea) |
disscusion about tensor and ndarrays
the difference between validation set and test set.
In machine learning, there are three sets: train set, validation set and test set. The train set is used for training the network/model (weights). The validation set is part of training process and it is used for parameter selection and avoiding overfitting (fine-tune the model). The test set is only use for performance evaluation about your model. discussion about this question
TensorFlow session.
A tensorflow session is used to compute a graph. It has three parameters: (target, graph, config). If no graph arguments is specified, the default graph will be lauched. One session can only run one graph, but one graph can be used in multiple sessions.
# Launch the graph in a session.
sess = tf.compat.v1.Session()
with tf.Session(graph=graph) as sess:
sess.run(tf.global_variables_initializer())
session.run([operation1, iperation2]): run this session and output the calculated result.
np.stack(); np.vstack(); np.hstack();
stack() is a function which join multiple arrays into one array/ sequence. When joining these arrays, the stacked array has one more dimention, and this dimention’s degree is the number of arrays.
stack((a,b,c), axis = 0/1/2); The first parameter is list of arrays. The second parameter is the dimention axis that increased. Return: The stacked array.
numpy.stack最通俗的理解