Skip to content
Merged

tf #2

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lessonThree/Anti-Fraud-App/VAE.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def read_data(source_file):
keep_prob = tf.placeholder(dtype=tf.float32, shape=(), name='keep_prob')

dec_in_channels = 1
n_latent = 8
#encoder输出的数据维度
n_latent = gConfig['encoutlen']
print(n_latent)
reshaped_dim = [-1, 7, 7, dec_in_channels]
inputs_decoder = 49 * dec_in_channels // 2

Expand Down Expand Up @@ -91,6 +93,6 @@ def decoder(sampled_z, keep_prob):

#保存训练的encode的结果,就是要进行特征压缩后的特征
sampled_data=pd.DataFrame(sampled_data)
sampled_data.to_csv('sampled_data.csv')
sampled_data.to_csv(gConfig['sampled_path'])


Binary file modified lessonThree/Anti-Fraud-App/__pycache__/execute.cpython-36.pyc
Binary file not shown.
Binary file modified lessonThree/Anti-Fraud-App/__pycache__/kmeansModel.cpython-36.pyc
Binary file not shown.
6 changes: 5 additions & 1 deletion lessonThree/Anti-Fraud-App/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
#
app = Flask(__name__)
#路由注解,我们这里使用的是path的形式进行传参
#示例url:http://0.0.0.0:8088/predict/1/2/3/4/5/6/7/8/9/10/11/12 这里的1...12换成需要进行聚类的值就可以了
@app.route('/predict/<a>/<b>/<c>/<d>/<e>/<f>/<g>/<h>/<i>/<j>/<k>/<l>', methods=['GET'])
def predict(a,b,c,d,e,f,g,h,i,j,k,l):
#获取url传来的需要进行预测的数据
line=[a,b,c,d,e,f,g,h,i,j,k,l]
lines=range(12)

k=len(line)
lines=range(k)
lines=[int(i) for i in line]
lines=[lines]
predict_result=execute.predicts(lines)
Expand Down
10 changes: 8 additions & 2 deletions lessonThree/Anti-Fraud-App/config.ini
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@

[ints]
#聚类的数量
k_num=10
num_clusters=10
#聚类最大训练的步数
num_epochs=10
#聚类训练步数
steps=10
#VAE训练步数
vae_steps=300
#需要聚类的序列的长度,如果要将vae和clustering串起来用这个值要和enc_out_len保持一致
seqlen=12
#vae encoder输出的中间向量的长度
encoutlen=8

[floats]
learning_rate = 0.01
[strings]
mode = train
working_directory=working_directory/
input_file=train_data/train_data.csv
model_path=kmeansMode/1542127988
#这里model的文件路径,要根据kmeansMode中的文件夹的来设置
model_path=kmeansMode/1542258970
sampled_path=train_data/sampled_data.csv



Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
20 changes: 12 additions & 8 deletions lessonThree/Anti-Fraud-App/kmeansModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,23 @@ def trainAndSaveModel(input_set,steps):

gConfig={}
gConfig=getConfig.get_config(config_file='config.ini')

input_fn = lambda: tf.train.limit_epochs(tf.convert_to_tensor(input_set, dtype=tf.float32), num_epochs=gConfig['num_epochs'])
kmeans = tf.contrib.factorization.KMeansClustering(num_clusters=gConfig['k_num'], use_mini_batch=False)

num_epochs=gConfig['num_epochs']
num_clusters=gConfig['num_clusters']
#定义input_fn函数
input_fn = lambda: tf.train.limit_epochs(tf.convert_to_tensor(input_set, dtype=tf.float32), num_epochs=num_epochs)
#实例化KMeansClustering
kmeans = tf.contrib.factorization.KMeansClustering(num_clusters=num_clusters, use_mini_batch=False)
previous_centers = None
for _ in xrange(gConfig['steps']):
kmeans.train(input_fn)#调用train对训练数据进行训练
centers = kmeans.cluster_centers()#保存质心
if previous_centers is not None:
print ('delta:', centers - previous_centers)
previous_centers = centers
print ('score:', kmeans.score(input_fn))
#将模型保存下来
modelPath = kmeans.export_savedmodel(export_dir_base="kmeansMode/",serving_input_receiver_fn=serving_input_receiver_fn)
print ("质心变化幅度:", centers - previous_centers)
previous_centers = centers
print ("模型评估得分:", kmeans.score(input_fn))
#将模型保存下来ˇ
modelPath = kmeans.export_savedmodel(export_dir_base="kmeansMode/",serving_input_receiver_fn=serving_input_receiver_fn)

print("训练完成,model文件存放在:")
print(modelPath)
Expand Down
28 changes: 28 additions & 0 deletions lessonThree/Anti-Fraud-App/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy as np
import tensorflow as tf

from six.moves import xrange

num_points = 100
dimensions = 2
points = np.random.uniform(0, 1000, [num_points, dimensions])

def input_fn():
return tf.train.limit_epochs(
tf.convert_to_tensor(points, dtype=tf.float32), num_epochs=1)

num_clusters = 5
kmeans = tf.contrib.factorization.KMeansClustering(
num_clusters=num_clusters, use_mini_batch=False)

# train
num_iterations = 10
previous_centers = None
for _ in xrange(num_iterations):
kmeans.train(input_fn)
cluster_centers = kmeans.cluster_centers()
if previous_centers is not None:
print ('delta:', cluster_centers - previous_centers)
previous_centers = cluster_centers
print ('score:', kmeans.score(input_fn))
print ('cluster centers:', cluster_centers)
10 changes: 10 additions & 0 deletions lessonThree/Anti-Fraud-App/train_data/sampled_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
,0,1,2,3,4,5,6,7
0,0.38341895,-1.7428886,0.29892516,0.14158645,0.043346316,0.52457184,-0.51498866,-0.0033487156
1,0.2401895,-1.6238832,-0.03735471,-0.32811102,0.85901475,0.61071527,1.7249069,0.19866326
2,-2.120377,-0.7846775,-0.44127777,-2.8433104,1.7282515,1.3949893,0.006724566,1.5689514
3,-0.24871545,-0.117795594,0.46943024,0.5850498,-1.8775313,0.5547079,1.5917208,-0.013330079
4,0.048848335,-1.1120182,-1.05481,1.7671635,-0.8837705,-1.0044992,-0.6401813,1.51355
5,0.23896244,1.2703017,0.56299645,-0.6899086,0.117588595,2.318331,0.55303776,-0.8386042
6,0.42145008,1.4267052,0.037153464,1.5952226,0.32282645,-0.17458007,1.6561109,-0.68001455
7,-1.9073356,0.4853596,-0.75159425,0.25134057,1.8075538,0.056467056,0.44442064,-1.3499131
8,-0.7253678,-0.53390133,0.10448843,0.15717143,1.7451864,-0.6664468,1.6801159,0.1358863
Loading