Preparation inTensorflow

Learning Tensorflow

Here I want to record the process of getting in touch with python, learning python and manipulating python finally. Fortunately, I have the chance to study tensorflow, a powerful tool to build the neural network. Therefore, I will take my time to understand the iceberg of python and machinery learning.

Week One(preparation):

Back to the point, the first week I tranfer my software from MATLAB to pycharm which is somehow challanging and suprising. After solving some problems about environmental configuration and the distinctive grammars, I can step into the fields of picture processing and matrix.

(1)Image processing

Read, write, display a “jpg.” or “png.” picture

import cv2A
img = cv2.imread("000.jpg",1)# parameter:1-name,2-[is0,gray,is1,colour]
cv2.imwrite("0001.png",img)#jpg和png封装格式:文件头和文件数据;    parameter:1 saved filename,2 data type
img2=cv2.imread("0001.png",1)
cv2.imshow("image",img2)# para:1-窗体type,2-imread读取数据
cv2.waitKey(0)#keep the window

Save picture with distortion and transparency

import cv2
img = cv2.imread("000.jpg",1)
cv2.imwrite("0002.jpg",img,[cv2.IMWRITE_JPEG_QUALITY,0])#set the quality of "0002.jpg"
#no distortion:1M compressed to 10K

Upper picture has 39.4K, bottom one only has 9.4K
在这里插入图片描述i
Read, set pixels of certain area

import cv2
img = cv2.imread("000.jpg",1)
(b,g,r) = img[200,500]# read the pixel of a certain point(200,500) in the picture
print(b,g,r)#opencv take the order of "bgr"

for i in range(1,100):
    img[10+i,100]=(255,0,0) #set pixel (pure blue,0,0) from (10,100) to (110,100)

cv2.imshow('image',img)
cv2.waitKey(1000)#continue the program after 1000ms

Block Summary:
1.picture element: 0,1 value, every 8 numbers on behalf of the color of a square
2.RGB: Red Green Blue in this fixed order||BGR(another order of color BGR)
RGB alpha(transparency);
3.Depth of color: measured in 0-255(8bit), totally 256 types of color
4.Wideth height: 640480: pixels * pixels
5.1.14M : 720 5473
8 bit/8(B) =1.14M

(2)Constant and variable in tensorflow

definition of constant and variable

import tensorflow as tf
data1 = tf.constant(2,dtype=tf.int32)#tf.constant(2.5) float type
data2 = tf.Variable(10,name="Var")

#Descriptive data
#Tensor("Const:0", shape=(), dtype=int32)
#<tf.Variable 'ar:0' shape=() dtype=int32_ref>
print(data1)
print(data2)

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

#Before running, sess must be defined
sess= tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(data2))
print(sess.run(data1))

Execution:Session and Equivalent usage

import tensorflow as tf
data1 = tf.constant(6)
data2 = tf.Variable(2)
dataAdd = tf.add(data1,data2)
dataCopy = tf.assign(data2,dataAdd) # assign the value of dataAdd to data2(Variable)
dataMul = tf.multiply(data1,data2)
dataSub = tf.subtract(data1,data2)
dataDiv = tf.divide(data1,data2)
init = tf.global_variables_initializer() #as data2 is a "Variable, it must be initialized and runned in Session
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(dataAdd)) # dataAdd = 6
    print(sess.run(dataMul))
    print(sess.run(dataSub))
    print(sess.run(dataDiv))
    print('sess.rundataCopy',sess.run(dataCopy))# 8->data2
    print('dataCopy.eval()',dataCopy.eval())# "eval()" equals to sess.run;
    # data2 = dataAdd = 6 + 8 = 14
    print('tf.get_default_sessione()',tf.get_default_session().run(dataCopy))
    # data2 = dataAdd = 6 + 14 =20


Pay attention to “Variable” before “sess.run()”, it must be initialised by “tf.global_variables_initializer()”.

rantionale of tensorflow arithmetic:
Tf: data + caculation flow
Tensor :all types of data
Op: operation
Graphs: operate the data
Session: an evironment to run the program

在这里插入图片描述

(3)Matrix

Placeholder:(a way of quoting function)

#placeholder
import tensorflow as tf
data1 = tf.placeholder(tf.float32)
data2 = tf.placeholder(tf.float32)
dataAdd = tf.add(data1,data2)
with tf.Session() as sess:
    print(sess.run(dataAdd,feed_dict={data1:6,data2:2}))
    # para:1-dataAdd;2-feed_dict={para1:6 para2:2}
print('end')

Matrix-1,denifition:

# matrix:M row N col  external:[row1,row2,...] internal:row1=[col1,col2,...]
#example:[[6,6],[5,5],[4,4]]  3by2 matrix
import tensorflow as tf
data1 = tf.constant([[6,6]])
data2 = tf.constant([[2],
                     [2]])
data3 = tf.constant([[3,3]])
data4 = tf.constant([[1,2],[3,4],[5,6]])
print(data4.shape)# print the dimension of the matrix

with tf.Session() as sess:
    print(sess.run(data4))
    print(sess.run(data4[0]))# print the certain row
    print(sess.run(data4[:,0]))# print the certain column
    print(sess.run(data4[0,0]))# print the site
print('end')

Matrix-2,operation:

import tensorflow as tf
data1 = tf.constant([[6,6]])
data2 = tf.constant([[2],[2]])
data3 = tf.constant([[3,3]])
data4 = tf.constant([[1,2],[3,4],[5,6]])
matMul=tf.matmul(data1,data2)#[1x2]matmul[2x1] = [1x1]
matMul12=tf.multiply(data1,data2)#[1x2]multiply[2x1] = [2x2]
matAdd=tf.add(data1,data3)# subtract and add comply the same op

with tf.Session() as sess:
    print(sess.run(matMul))
    print(sess.run(matAdd))
    print(sess.run(matMul12))
print('end')

Matrix-3,special ones:

import tensorflow as tf
mat1 = tf.zeros([2,3])
mat2 = tf.ones([3,2])
mat3 = tf.fill([2,3],15) # form a matrix with all elements(para1,dimension) being para2
mat4 = tf.constant([[2],[3],[4]])
mat5 = tf.zeros_like(mat4) # _like: render mat5 the same shape as mat4
mat6 = tf.linspace(0.0,2.0,11) # 11 points equally divide this innterval[0,2]
mat7 = tf.random_uniform([2,3],-1,2) # generate a random matix with shape of para1[2,3], values confined in (-1,2)
with tf.Session() as sess:
    print(sess.run(mat1))
    print(sess.run(mat2))
    print(sess.run(mat3))
    print(sess.run(mat5))
    print(sess.run(mat6))
    print(sess.run(mat7))

Matrix4-numpy:

import numpy as np
data1 = np.array([1,2,3,4,5])
print(data1)
data2 = np.array([[1,2],[3,4]])
print(data1.shape,data2.shape)
print(np.zeros([2,3]),np.ones([2,2]))
data2[1,0] = 5 #rewrite
data3 = np.ones([2,3])
data4 = np.array([[1,2,3],[4,5,6]])
print(data3*data4) #correspondent multiply

Block Summary:

  1. The parameter of a def can be replaced by placeholders , to make the assign clause put off.
  2. Compared with tensorflow, numpy probably is more brief and simpler when operating matrix.
  3. The format of setting a matrix are fixed with “[[],…]” ,however other operations are more likely to be the same as in MATLAB.
(4)Matplotlib
import matplotlib as mpl
mpl.use('TkAgg')  #individual problem, because in this version the pop-ups of figures are cancelled

import matplotlib.pyplot as plt
import numpy as np
x = np.array([1,2,3,4,5,6,7,8])
y = np.array([3,5,7,6,2,6,10,15])
plt.plot(x,y,'g',lw=10) # draw a line chart,para3-color;para4-linewidth
#plt.show() # make two chart showed in one figure
plt.bar(x,y,0.9,alpha=1,color='b')# para3-bar width;para4-transparency;para5-color
plt.show()

在这里插入图片描述

This article quotes many other courses and website instructions and doesn’t have the originality. It is just the pleasure to share my study experience in CSDN. Next time, look forward to building a neural network with tensorflow as the window has been opened in python.

written by Horace , a member of our SRP team of SCUT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值