Tensorflow(0)

Tensorflow(0)

 

Overall understanding:

  1. Tensor, i.e. tensor, i.e. high dimensional array; Flow, i.e. the flow, showing the relationship between the tensor and an operation; tensorflow between tensor is calculated.
  2. Calculated between the tensor is implemented in FIG. (Graph) in

Figure:

  1. FIG side and is composed of points.
  2. Node, i.e., the operation (Operation); operations may be arithmetic operations, may be initialized to a constant, variable.
  3. Is connected to edge nodes, the data is transmitted and represents the dependency between a control node; and a solid line shows a conventional side, i.e., only data transfer is connected to the node on the graph; special tables dotted lines, i.e. the presence of the node is connected before and after the execution of dependent relationship.
  4. node1 = tf.constant(3.0, tf.float32, name='node1')
    node2 = tf.constant(4.0, tf.float32, name='node1')
    node3 = tf.add(node1, node2)
    

    Calculating a relation established as above, we will give the corresponding Graph;

  5. Note that direct printing node3, 7.0 will not be displayed, but Tensor ( "Add: 0", shape = (), dtype = float32); Tensor i.e., returns a value that includes the node name, and the type of shape; To show node3 the specific values ​​need to be in the session, when TF only executed session, in order to provide data and display the results.

Tensor

  1. TF All data are tensor; tensor is essentially an array: zero-order tensor is a number, the first order tensor is a vector of a one-dimensional array, n order tensor is n-dimensional array; but not directly tensor a table array storage, calculation is saved.
  2. Tensor ( "Add: 0", shape = (), dtype = float32), wherein: the first term is the name before the colon indicates the specific name, such as meant here is an addition, after the colon is the serial number, indicating the first few additions calculated; the second dimension is the default for the blank, at this time is a scalar; is the third type, the type is extremely important prerequisite for the type of match is calculated.
  3. Tensor ( "Add: 0", shape = (), dtype = float32) in shape: a tensor parentheses dimensional shape of a few, there are a few numbers (determined by the number of number in parentheses), and by the intrinsic every dimension take the number of elements in parentheses; such as: [1,2,3] -> shape (3) and a one-dimensional first dimension has three elements; [[1,2,3], [4,5,6]] -> shape (2,3) and the first two-dimensional element of the second two-dimensional three-dimensional elements.
  4. tensor can be like an array of values, such as second order tensor, may be obtained in the form of an element t [i, j], the same index is from 0 to.
  5. Tensor type is the default, the program will fill in their own judgment based on the obtained value. If the value passed without a decimal point, it is judged int32; if the passed value has a decimal point, the judge is float32; due to the presence of default, must pay attention to first determine the type matching the calculation, otherwise the program is prone to various errors .
  6. When you define a tensor although the name may default, but best to bring, because it can have on Tensorboard better readability.

operating

  1. Operation is represented as a node in the calculation of FIG.
  2. Arithmetic operation may be the operation may be initialized constants, variables.
  3. Operation may be bound to different devices, which may be provided or multiple cpu gpu gpu binding calculated.

Conversation

  1. The session is to integrate the resources used to run TF, TF all calculations and displays the results were achieved in the session, but note that at the end of the program should promptly close the session to prevent memory leaks.
  2. tens0 = tf.constant([1,2.3])
    sess = tf.Session()
    print(sess.run(tens0))
    sess.close()
    Session may be introduced as described above; however, a problem in the above code, an abnormality before the session is closed once the sentence, the program stops midway, the session will not release properly.
  3. tens0 = tf.constant([1,2.3])
    sess = tf.Session()
    try:
        print(sess.run(tens0))
    except:
        print('Eception!')
    finally:
        sess.close()
    Python can be used to improve exception handling statements to introduce the session code. The code will execute in the session close operation under which circumstances.
  4. tens0 = tf.constant([1,2.3])
    with  tf.Session() as sess:
        print(sess.run(tens0))
    
    Python may be more general context session manager is introduced. In this situation, no matter how you leave the area, a normal conversation is always released.
  5. sess = tf.Session()
    with sess.as_default():
        print(tensor.eval())
    TF will generate run-time default graph, but does not produce the default Session; you can use statement specifies the procedures carried out under the default session. In the default session, using tensor.eval () calcd tensor, the effect sess.run (tensor) uniform, but non-default error when using the eval function session.
  6. sess = tf.InteractiveSession()
    In python, can be achieved with the introduction of the default session a statement.

Constants and Variables

  1. constant_name = tf.constant(value,nmae='constant_name')
    Constant definitions indicated above, the definition of which is preferably consistent with the name defined in the name of the constant; defined by a constant does not change, and no additional initialization operation; however, performs a correlation operation and the output value should be in the session.
  2. var_name = tf.Variable(value,nmae='vart_name')
    
    init = tf.global_variables_initializer()
    sess.run(init)
    Defined constants is no different, the main attention capitalized. All variables are initialized before use to operate; and note that there is often a calculation of the variable dependencies, the order of certain arrangements good has calculated.
  3. epoch = tf.Variable(0, name='epoch', trainable=False)  # 不更新
    
    update_op = tf.assign(variable_to_be_upadated, newValue)  # 人工输入值给变量
    Neural networks, the weights are in the form of variables, because the variable is defined by a human generally will not pass the value of the variable itself will be updated as training; variable for the code above may not want to update the parameter set to False trainable ; the need for variable assignment under special circumstances, you can use the above tf.assign make changes, the first parameter variables to be changed, times the value of the parameter to be entered.
  4. value = tf.Variable(0, name='value')
    one = tf.constant(1, name='one')
    new_value = tf.add(value, one, name='new_value')
    update_op = tf.assign(value, new_value)
    init = tf.global_variables_initializer()
    
    
    sess = tf.Session()
    with sess.as_default():
        sess.run(init)
        for _ in range(10):
            sess.run(update_op)
            print(sess.run(value))

    The code assignment to achieve using tf.assign outputs 1-10

  5. import tensorflow as tf
    tf.reset_default_graph()
    value = tf.Variable(0, name='value')
    one = tf.constant(1, name='one')
    new_value = tf.add(value, one, name='new_value')
    update_value = tf.assign(value, new_value)
    sum = tf.Variable(0, name='sum')
    new_sum = tf.add(sum, value, name='sum')
    update_sum = tf.assign(sum, new_sum)
    init = tf.global_variables_initializer()
    
    sess = tf.Session()
    with sess.as_default():
        sess.run(init)
        for _ in range(10):
            sess.run(update_value)
            sess.run(update_sum)
            print("value: ", sess.run(value))
            print("sum: ", sess.run(sum))
    
    
    logdir = 'E:/log'
    
    writer = tf.summary.FileWriter(logdir, tf.get_default_graph())
    writer.close()

    The code achieved .. + 10 + 1, using the same variable tf.assign update.

Placeholder

  1. For the beginning of the unknown variables, expressed as a placeholder; is a placeholder for the variable nature, it needs to be initialized before use is; in the neural network, the input data is often expressed as a placeholder.
  2. x = tf.placeholder(dtype, shape=none, name= none)
    When you define a placeholder, it is usually optional parameter type, dimension and name; which name, the dimension is the default, but it is best to write the name of the placeholder name to improve readability; the type is essential Province.
  3. result = sess.run(operation, feed_dict={intput1:a,input2:b ...........})
    If the operation is dependent on a placeholder, during operation, it should be used to correspond to the incoming feed_dict initial value placeholder; an incoming, implemented in a dictionary.

 

 

Tensorboard

  1. tensorboard is based tensorflow log visualization tool for information extraction; tensorflow can work with in a different process in.
  2. 1.tf.reset_defualt_garph()  # 清除默认图中的计算
    2.logdir = 'E:log'  # 在指定路径下建立日志文件夹
    3.定义各种计算......
    4.writer = tf.summary_FileWriter(logdir, tf.get_defualt_graph())
      writer.close()
    ******************以上完成了对定义计算图,并生成日志************
    
    ******************以下读取日志,并可视化***********************
    1. 在adacoinda的prompt中操作
    2.首先切换到日志所在路径
    3.使用 tensorboard --logdir==日志路径
    4.命令执行后,会最终弹出一个地址,利用这个地址在浏览器进入
    Visualization of the four main steps.

 

 

Released five original articles · won praise 0 · Views 66

Guess you like

Origin blog.csdn.net/weixin_41707744/article/details/104659188