Deep learning day03-numpy uses and feels the composition of images

As a good helper for deep learning, numpy is a third-party library of the Python language that supports a large number of high-dimensional array and matrix operations. In addition, NumPy also provides a large number of mathematical functions for array operations. Machine learning involves a large number of transformations and operations on arrays, and NumPy has become one of the essential tools.

A brief introduction to the basics of numpy:

NumPy's main object is the multidimensional array Ndarray. In NumPy, the dimension Dimensions is called the axis Axes, and the number of axes is called the rank Rank. Note that numpy.array is not the same as the Python standard library array.array, the former is more powerful, which is one of the important reasons why we learn NumPy.

Parameters using 
numpy.array:

 
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
 
parameter meaning
object Any object that exposes an array interface method will return an array or any (nested) sequence.
dtype  The desired data type of the array, optional.
copy  The default is true, whether the object is copied, optional.
order  C(by row), F(by column), or A(any, default).
subok  By default, the returned array is coerced to be a base class array. If yes true, return the subclass.
ndmin  Specifies the minimum number of dimensions for the returned array.

demo1: one-dimensional array

a = np.array([1,2,3]) 

#输出 [1, 2, 3]

demo2: two-dimensional array

a = np.array([[1,  1],  [2,  2]]) 

#输出 : 
[[1, 1] 
 [2, 2]]
 

NumPy also supports a wider variety of numeric types than Python:

bool_
A boolean value (true or false) stored as a byte
int_
intc
intp
Default integer, equivalent to C's long, usually int32 or int64
Equivalent to C's int, usually int32 or int64
Integer for indexing, equivalent to C size_t, usually int32 or int64
you8
int16
int32
int64
byte (-128 ~ 127)
16-bit integer (-32768 ~ 32767)
32-bit integer (-2147483648 ~ 2147483647)
64-bit integer (-9223372036854775808 ~ 9223372036854775807)
uint8
uint16
uint32
uint64
8-bit unsigned integer (0 ~ 255)
16-bit unsigned integer (0 ~ 65535)
32-bit unsigned integer (0 ~ 4294967295)
64-bit unsigned integer (0 ~ 18446744073709551615)
float_ 
float16
float32
float64
Shorthand for float64
Half-precision floating point: sign bit, 5-bit exponent, 10-bit mantissa
Single-precision floating point: sign bit, 8-bit exponent, 23-bit mantissa
Double-precision floating point: sign bit, 11-bit exponent, 52-bit mantissa
complex_  
complex64 
complex128
Shorthand for complex128
Complex numbers, represented by two 32-bit floating points (real and imaginary parts)
Complex numbers, represented by two 64-bit floating points (real and imaginary parts)

Numpy numeric types are dtypeinstances of (datatype) objects, each with unique characteristics. These types can be np.bool_, np.float32etc.

A little understanding is good, the specific numpy super detailed tutorial can be seen:

https://blog.csdn.net/qq_52213943/article/details/123722568https://blog.csdn.net/qq_52213943/article/details/123722568https://blog.csdn.net/qq_52213943/article/details/123722568

Then start today's code:

First we import TensorFlow, numpy, matplotlib,
then we write a function using the canvas recorded yesterday, pass in a tensor we constructed, convert it into an array and then create a canvas and display it (see day01, day02 for the principle) first import TensorFlow,
numpy , matplotlib

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt



def show_tensor(tensor):
    b = np.array(tensor)
    plt.figure()
    plt.imshow(b,cmap='gray')
    plt.show()

Define the main function, create a tensor of 128, 128, 3, all values ​​​​are 0

if __name__ == '__main__':
    a = tf.zeros([128, 128, 3])
    b = np.array(tf.zeros([128, 128, 3]))
    show_tensor(a)

Call this tensor to the previous show_tensor() to get a canvas image that is all black

Now let's modify some, we change the value in a range from 0 to 255 (RGB three-color channel, 0-255 pixel value, 0 is black, 255 is the brightest is white):

 for i in range(20,100):
        for j in range(20,100):
            b[i,j,:] = 255

 Then print it out to see:

The point where we change the value from 0 to 255, changes.

Let's be more random:

 d = tf.zeros([128,128])
    d = np.array(d)
    d[35:50,45:80] = 125
    d[12:20,56:68] = 225
    d = tf.constant(d)
    show_tensor(d)

 Guess what he looks like?

He looks like this:

In the same way, I guess you can now use numpy to draw arbitrary images, such as:

More complicated, of course, you can try it yourself!

 Perhaps there are no miracles in this world at all, but if an incredible change really happens to you, isn’t it the biggest miracle in the world to you? May you too Meet that milestone as soon as possible, and then break out of the ground to create a miracle of your own life!
 

Guess you like

Origin blog.csdn.net/qq_52213943/article/details/123721790