## Neural Network

TensorFlow中文社区

tensorboard使用

Tensorflow 模型文件的使用以及格式转换

tensorflow只能在python3.5和python3.6安装，踩坑就踩了好久。

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

```t0 = tf.matmul(In_string, v0)
t2 = tf.sigmoid(t1)
t3 = tf.matmul(t2, v1)
t5 = tf.tanh(t4)
t6 = tf.matmul(t5, v2)
t8 = tf.sigmoid(t7)```

matmul：矩阵相乘

sigmoid：S型生长曲线，具体可以百度

tanh：双曲正切

relu：正数不变，负数变为为0

```def gn(nm):
return sess.run(nm + ":0", feed_dict={x:[X]})
In_string = gn("In_string")

```def gv(v):
return sess.run(v, feed_dict={x:[X]})
print(gv(a0))```

```np.set_printoptions(threshold='nan')
np.set_printoptions(precision=20)```

```from scipy.special import logit
def invsigmoid(a):
b= np.array([[0.0]*32])
for i in range(32):
b[0][i]=logit(a[0][i]) #logit为sigmoid的反函数，但是tensorflow中没有这个函数，只能借助scipy中的了
return b
a = np.array([[1.40895243e-01, 9.98096014e-01, 1.13422030e-02, 6.57041353e-01,
9.97613889e-01, 9.98909625e-01, 9.92840464e-01, 9.90108787e-01,
1.43269835e-03, 9.89027450e-01, 7.22652880e-01, 9.63670217e-01,
6.89424259e-01, 1.76012035e-02, 9.30893743e-01, 8.61464445e-03,
4.35839722e-01, 8.38741174e-04, 6.38429400e-02, 9.90384032e-01,
1.09806946e-03, 1.76375112e-03, 9.37186997e-01, 8.32329340e-01,
9.83474966e-01, 8.79308946e-01, 6.59324698e-03, 7.85916088e-05,
2.94269115e-05, 1.97006621e-03, 9.99416387e-01, 9.99997202e-01]])
a0=invsigmoid(a)
a1=tf.subtract(a0,v5)
a2=tf.matmul(a1,tf.matrix_inverse(v2))
a3=tf.atanh(a2)
a4=tf.subtract(a3,v4)
a5=tf.matmul(a4,tf.matrix_inverse(v1))
a6=invsigmoid(gv(a5))#a5是个tensor对象，要用np.array数据类型才能使用。
a7=tf.subtract(a6,v3)
a8=tf.matmul(a7,tf.matrix_inverse(v0))
l=gv(a8).tolist()
ll=l[0]
flag=''
for i in range(len(ll)):
ll[i]*=128
ll[i]=round(ll[i])
flag+=chr(ll[i])
print(flag)```

```def trans(secret):
return np.array([float(ord(x))/128 for x in secret])
fakeflag = "flag{01234567012345670123456789}"
X = trans(fakeflag)
sess = tf.Session()
saver = tf.train.import_meta_graph('model.meta')
saver.restore(sess, tf.train.latest_checkpoint('./'))
graph = tf.get_default_graph()
x = graph.get_tensor_by_name('In_string:0')
y = graph.get_tensor_by_name("Out_judge:0")
res = sess.run(y, feed_dict={x:[X]})
a0=invsigmoid(res)
a1=tf.subtract(a0,v5)
a2=tf.matmul(a1,tf.matrix_inverse(v2))
a3=tf.atanh(a2)
a4=tf.subtract(a3,v4)
a5=tf.matmul(a4,tf.matrix_inverse(v1))
a6=invsigmoid(gv(a5))#a5是个tensor对象，要用np.array数据类型才能使用。
a7=tf.subtract(a6,v3)
a8=tf.matmul(a7,tf.matrix_inverse(v0))
l=gv(a8).tolist()
ll=l[0]
flag=''
for i in range(len(ll)):
ll[i]*=128
ll[i]=round(ll[i])
flag+=chr(ll[i])
print(flag)```

```sess = tf.Session()
saver = tf.train.import_meta_graph('model.meta')
saver.restore(sess, tf.train.latest_checkpoint('./'))
graph = tf.get_default_graph()
tf.train.write_graph(graph, './aaa', 'train.pbtxt')```

```node {
name: "PRECISEFINAL"
op: "Const"
attr {
key: "dtype"
value {
type: DT_DOUBLE
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_DOUBLE
tensor_shape {
dim {
size: 1
}
dim {
size: 32
}
}
tensor_content: "!333736633210302?3068crg360357?02\336266224:207?367s226226{06345?211224C366s354357?tkYQ21367357?303CE]Y305357?20705215237370256357?30344262",yW?320732534434246357?26733533635637037347?367345j354b326356?|34lv30317346?210242s3061406222?372353304254341311355?245\21401216244201?%254@J314344333?30?251364336{K?k24R31302X260?331361R3329261357?27U36433243375Q?#31dW265345\?265b315225o375355?22207375#q242352?H330273}240x357?A362214203L#354?L00207B20501{?32334403217123224?03222)3659333376>372241322=207#`?36132424238373357?3713327!372377357?"
}
}
}
}```

```import os
import numpy as np
import tensorflow as tf
from math import isclose
from scipy.special import logit
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
np.set_printoptions(precision=20)
np.set_printoptions(threshold='nan')

def trans(secret):
return np.array([float(ord(x))/128 for x in secret])
def gn(nm):
a=graph.get_tensor_by_name(nm + ":0")
return sess.run(a, feed_dict={x:[X]})
def gv(v):
return sess.run(v, feed_dict={x:[X]})
def invsigmoid(a):
b= np.array([[0.0]*32])
for i in range(32):
b[0][i]=logit(a[0][i])
return b

fakeflag = "flag{01234567012345670123456789}"
X = trans(fakeflag)
#print(X)
sess = tf.Session()
saver = tf.train.import_meta_graph('model.meta')
saver.restore(sess, tf.train.latest_checkpoint('./'))
graph = tf.get_default_graph()
x = graph.get_tensor_by_name('In_string:0')
y = graph.get_tensor_by_name("Out_judge:0")
final = np.array([[1.40895243e-01, 9.98096014e-01, 1.13422030e-02, 6.57041353e-01,
9.97613889e-01, 9.98909625e-01, 9.92840464e-01, 9.90108787e-01,
1.43269835e-03, 9.89027450e-01, 7.22652880e-01, 9.63670217e-01,
6.89424259e-01, 1.76012035e-02, 9.30893743e-01, 8.61464445e-03,
4.35839722e-01, 8.38741174e-04, 6.38429400e-02, 9.90384032e-01,
1.09806946e-03, 1.76375112e-03, 9.37186997e-01, 8.32329340e-01,
9.83474966e-01, 8.79308946e-01, 6.59324698e-03, 7.85916088e-05,
2.94269115e-05, 1.97006621e-03, 9.99416387e-01, 9.99997202e-01]])
res = sess.run(y, feed_dict={x:[X]})
In_string = gn("In_string")
precisefinal=gn("PRECISEFINAL")
a0=invsigmoid(precisefinal)
a1=tf.subtract(a0,v5)
a2=tf.matmul(a1,tf.matrix_inverse(v2))
a3=tf.atanh(a2)
a4=tf.subtract(a3,v4)
a5=tf.matmul(a4,tf.matrix_inverse(v1))
a6=invsigmoid(gv(a5))
a7=tf.subtract(a6,v3)
a8=tf.matmul(a7,tf.matrix_inverse(v0))
l=gv(a8).tolist()
ll=l[0]
flag=''
for i in range(len(ll)):
ll[i]*=128
ll[i]=round(ll[i])
flag+=chr(ll[i])
print(flag)```