parent
b1796de6fc
commit
8d4ceded8d
File diff suppressed because one or more lines are too long
@ -1,12 +0,0 @@
|
||||
{
|
||||
"type": "service_account",
|
||||
"project_id": "aou-res-curation-prod",
|
||||
"private_key_id": "ecbf77975c5b7b1f4d4b1680bf67a5e0fd11dfaf",
|
||||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDcDBFIIOYtSlKU\njA5xBaRYk6+jccisNrvfBT3Lue9Kqy6ad35V+gUGqZ18bIKAgziQy8m5Iiw5VDFm\nZslN4yUFy05g0k+XY0HYq2rqeMeThH9pQ/xfN6XsOy8ED9eONzvnhuX2Jbevm9qr\n0xSIAkFsBYGNs+NjgU4fSmLfptAt2rVs19BI1qRPVBBzA640+hWIATVqr0ibnpE7\ndl7ONzWPWgmgHfhu2A1MjYO5obNJ4NyZ8Y86aAa7N098r+388QGX3XN2I9rWfKIj\nUGEpapmEKwnb8zC92v9GaT2qfbO2vdNsRYE930LGmxlmW+Ji7YO5FRaitvuk2iMU\n7g8d/GZZAgMBAAECggEAIUXi3Bb7byhAne0ipuTtQaaNRafoKeA53sJ+Yl6aaB5D\n1QASFqKQXX5KzbxjrFaLOFvURB3+dWm9cYhD0rbwy3Q/RQUwG0pbM83RxCQgu3Xq\nxSpOUECMIpEdbh4OIFdKQ9tiTOrNoGxu75HiliFPLqwTd6+Wh96Ox0z6b+qbqn8S\nqcEK0JQXvzC1BbR7vhsySIFP5hz8F0JThm94B3tiClzsixGCk6wydXuPs64x3rGt\nZ57dxBQBUVxYmaI3LQ/1cm7nv7uqfbUHDZrpLzE6/AevP5iNyzY1bkdUJ45mj2Ay\nWhqW9ftOhyRE9C2djPcopgrjRPbH/U0491tTLuc2XQKBgQDyp08o7mEz97/aGWmr\nNj3+QjBwNoDkdiR3qUrgohLwSNahSpiPv9+yjGvfXHQUxNyJfJ2Zei5bSTCjqKTk\nNq4QmvO4gsEhABOuqU0U0NlrpGSj0amwrCrqh7gxG/tnSuVEOzEKbY9g0CaXlg1O\nbJtP8yvicJc7m/5RxLKI8LoW1QKBgQDoJnKv8+JZc/16FI/4bU8BwUUHRiazWDIt\n9aCt63h+Fs6PAAFuGo04lobQEukbwU3EB63jWKCaxGJkjh+/lLkTelzRlVyVs0N0\nOb9WL4vYtwMrmtXKPfqKmJS81qwlLHA0+YBeE56uElwyFMAEsIIRb4YjffZd3Cy9\nT19cMSmbdQKBgBo046HCDRF1wmylrfnlw9BACcc0u7rw34Nk70dPecgltbh5u/xa\ndqhr7gKTk53inQbkRIkc3wDQ6MXkItra5PW6JnRY+s67mWSVuFN1MuYjPRNMQ41n\nKsNloQj8wqwnNJen5OYBayjDkkdw10MPC78YvjaYflzbvh3KppWPmil5AoGBAKID\nWxyynrABA9A0E3mzh2TZJbx6171n+rUaa8WUxKVycztXLKhTfWUVoAYMfISzNftt\nxIwaKRN5pJU6nquMNlGCns5hZ5jN33B4cLDMQ9O9fUfsKfGXqYcaDwtu4fqbdb9y\ntIRzOtWO2KrW0l8zc8KJS1rvqIU+iDah8xIa+UeVAoGBAKagVX0AN1RwoRg7LKdZ\n9eMQeYfaeVrfbxMDqEluEJzAQbvRoPZ75UNMre+vTOHLZuPF9uT+N71amgkKaL1T\nV1qWzNBU0bvpD9xvdCJWmypoccV2by1Nj2rPll5wfg1CPhmEQuNB30YLOTAws9Tc\nmb0kWAwnL39cUQyXJ5zBGd3K\n-----END PRIVATE KEY-----\n",
|
||||
"client_email": "aou-res-curation-prod@appspot.gserviceaccount.com",
|
||||
"client_id": "",
|
||||
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
||||
"token_uri": "https://oauth2.googleapis.com/token",
|
||||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
||||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/aou-res-curation-prod%40appspot.gserviceaccount.com"
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
{
|
||||
"type": "service_account",
|
||||
"project_id": "aou-res-curation-test",
|
||||
"private_key_id": "be9cb7427212dea882379d125530f5339ba854a7",
|
||||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCljt1hxwqInD2U\nKLv9SQX08tE0+APKzOH4Cw3vZt495aAlmKpsRt0ze3HdobouOQXzySqJZqHqgK3k\n5oqjlXOEFVrupIO5WnujGA3Ln7SP9vK1fjiNeKFvX/W+ePULRsOp1pZts53p5OzU\nS2PU2UowAVip9iJAjTeLpoF4cYjHG1jM4oYIRq8mCtuBNmsNE6peY4lWrlouHIvy\nPKJOAQ0kwEbtxsVfEBYqvcb8X5NSFi4/gwP5y1z8ALjQ3eLJjcqPfsAGI2Lpf7Ah\nM+RbW3rkT0FKCbUjUY1NNhQKguDdzeTGModjGyQxp3Y7qT1LHOvRKIZXb3Ft3f1C\nHyUsytJlAgMBAAECggEAEk8TXS3VlLgFTgOXOUfrGGSwuDU5Y3h3APxlT8rGMdLZ\nfBmUYfcQSBI9zG8x7MyyQ3yaxKk3Uidlk8E0fH9u+qDLS/BLqk2UYLwB7Tk95FyW\nCMuVq3ziCt7HiYdM6jCq5iHCGbhZyApgxTWKgSPVQtZ98gXd/IThgK3VoaFEqWgc\nsVDO/AokZF56luDHzISALh3+LhsoYxerTP43XA4jDv4i/qzmDAwUcBf1mI76qaOZ\nOwoETJre+kaI61ZqVcnGteSVnvfb8Z5e5Gvwtig2akiNbT+E/HeTfQiCTJ9r5dUA\n5U1r4O4Veu0gLENvK4NE0kdn3k6BTLeOljuxIXzHPQKBgQDk2/0SdhZYScAsXts+\nFl3FJbVU409szRX/uUWtBjD2sIm9GRYmBv07Kk3MV+Egh8e9Ps/wjb6fxbhlEVGf\nvbPuR9pn4Ci+fllH7TWsy1atcyZaZXD22/eHOXOjiE+rFUAfO94fXIxVXtB0yuxe\nf+zQ6rltpn5ttZBQghYsm0weawKBgQC5MRftZqf+d7AzP5Pj4mPUhxmwrHNWaryw\nHAqer4X9kjS/zBlHWQjdqA9rpFgXzaETRY5sbC9ef2FgCG2mSAYEvyYBA4L8t71s\ntUO/v3VgSs0xheOnAI8RFnq5g5Bbzd4IPZB+dEq9gPph+P/QLCFpRX0LFzhOwkrx\nvaicvMFmbwKBgHFL5tEI3K8Ac76Dhw4JjIpYzJgln+BA9y8NzUyG0B6P7uBKVwik\nVSDBJJqQtsaf8WXifpab1U7LVynRlRL7muPPdnQOKJ2FdzWAXR4Z2+MqKkZ+CZpr\n8vJiorjGdoo/jurnfGMSMfbhZVksTC/MLLSQPxPlZJlzVOpGPCwBBYHZAoGALmKE\nirresxcJdByljzuiI5ZfMehPz0JW1ol/g3WVSwj2219kqYE8fkBc9GoqgnPHt4sB\nfFiwmKuxGRujUzXRBBlYjIJzqZbgBD12pa1v2dmCgbf2aFr0eqQ1wweX/daXmVrK\nOVIpckO+8xEqCdsz1ylHg6KiQN/bY6dMd02z51MCgYAZRr+hutgjPOQEdWujFgkL\noTypuWmqvdQmOj8L0o3wee2D5ScMx7obtoKhYP+FpC4U9xgoImiS9hLL3FurwVNi\n36GEodFO7iTFnBowJFp+COW5xX8ISEc0LVKkFoHyMfXZa+zxFWRTRRRwzmnHGBq+\nRY2vrlcCx36QEcQdwFR72Q==\n-----END PRIVATE KEY-----\n",
|
||||
"client_email": "aou-res-curation-test@appspot.gserviceaccount.com",
|
||||
"client_id": "",
|
||||
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
||||
"token_uri": "https://oauth2.googleapis.com/token",
|
||||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
||||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/aou-res-curation-test%40appspot.gserviceaccount.com"
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
{
|
||||
"type": "service_account",
|
||||
"project_id": "aou-res-curation-test",
|
||||
"private_key_id": "1ed8d298e4b5572e7556b2f079133ea04568396a",
|
||||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCwf6bQeRLpKhlh\nvFIjiZvdu68mm/6x37zmH5jBoCv8fWteUbqSwyPzz3954qLpENvad/ob+ELHMOnU\n9LNgRwKTYNuBpRRPELdh95lF7zmHyat6GRA0Y6ofIU1ScjzJlQlFQ8+PnYNTrpgh\nqOtmgLAbI7S0mMsQodXsZwuPyHW0nf5CCY7gnXuThxmTt46hY7Zd9WCKH7ra+P9W\nqzHRRQyuC/43bQIFuHP1wWUrmnJCbypWcgRQEKfoWyXKVEsNGcbUMYLFOixraQuk\n8mhRgncMe2dR7R2M60Q0fLKAF4CZDfG2N70ACoIY8sFGkXSjFG2rlok7spYwdYIX\nafKQHbZ1AgMBAAECggEAAzb7NfSs/5psACFuFsY3+xFvyF9ZNvcxMx9wzyU/BKg2\n9buKXCFgY12S3+72jBIDcL0ns2CE76jet9zFjNbheQeJTmXp2UjS9kTywaXXIYSI\nWL7h6/pdJZg1ltW/pEvp8WnuewCukC5WO6K4HiCKh9Jq+H3uxCMWfB0iX+BevuC4\n4FEC0eJ6BD5rI8gUr5HO8VtCgxW99dJHgrdx+rRlJEaeY5FwGLW/ITBjsV0S48Pl\nvxcpHWbUCn13tE1EWR0QhFyazUWw8xqdY1+H++ku45pAZuMxYlFGkhssBbVJqYwP\nMjkjy9z4n/xXUVj7iwTWweQ2dvZracKEmBP2szAJkQKBgQDeGXMRJ9UK79Anewm/\nqXQWKzMYzwcWT+1hTfUweL5SMzTjO/tuqMsDqndCeAacpLUOxXjIcINWP/P1+VDH\nIFj8JpQMw86t2JUwMqcmSk44he85wfT3qgoxe6LglQIbWgV6cZY1OKnkuKIln2FW\nlpGdiSRRM430+wN2Fq9YsFRimQKBgQDLcFlBVL0NTC+wlD72I6/dSKeWNpz50epp\ng4qOg3zq7rMa8S/O/m1w3ZFyeAz+E4LA1Mi42MQPr6vhGFPczQgoPboe2rCnIPqR\nnFhkWqLBTk7BgmqnZV1lzrdvosmGscOdfQwnw8gNDe1KjAmPQvdP95qGcYKh5kKu\nxz3P3S74PQKBgAZ9YeJfcpc2OLPeoYNLNUwsiPqxmfhp73rHZ2G6NX17Z5E4QHmU\nTxJVWdTEYxUSrwO2e3gH6Z6MkdlfJqAa7t63Vd4lnpVv3bQh1saEp1J5f2sFot3V\nxyR5A2JimEQqVjykswntFPHM/1fwF00La0faKQiCZCSDbS93LDqANIcJAoGBAJmE\nc2YweuVA+6/lfsmhToHO5OAe4EBI3vq2j+VRZf+nFzMalDhAmPeVy780xqEouf+n\n0rxinzkzGKIpCIfTlPdA9WV5I9tKsKsW70DzgGQdIqM2NiOSA3PjFVvB3Q+ur231\nwilzvU/UlZ8uo7wfDZ+julD/8VMY/nMD2So1v88FAoGACPUobP69SukbZIl4rYLL\nAZEcxlQCOP/2nWGY7spReiIZKqXCkwMElR8r41//Kb6/h0knKlW8NsC2vpvOBgHO\nG7ZYooscHP8v203lPtGykaBA1xeFY5NKD0gGAG+CmSLorM8cYMUv4RXrIOtmAgrG\nXdLo0jPwQXGSTqOdPvNqBi0=\n-----END PRIVATE KEY-----\n",
|
||||
"client_email": "aou-res-curation-test@appspot.gserviceaccount.com",
|
||||
"client_id": "",
|
||||
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
||||
"token_uri": "https://oauth2.googleapis.com/token",
|
||||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
||||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/aou-res-curation-test%40appspot.gserviceaccount.com"
|
||||
}
|
@ -1,286 +0,0 @@
|
||||
import tensorflow as tf
|
||||
from tensorflow.contrib.layers import l2_regularizer
|
||||
import numpy as np
|
||||
import time
|
||||
import os
|
||||
|
||||
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
|
||||
|
||||
#### id of gpu to use
|
||||
os.environ['CUDA_VISIBLE_DEVICES'] = "0"
|
||||
|
||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
||||
|
||||
#### training data
|
||||
#### shape=(n_sample, n_code=854)
|
||||
REAL = np.load('')
|
||||
|
||||
#### demographic for training data
|
||||
#### shape=(n_sample, 6)
|
||||
#### if sample_x is male, then LABEL[x,0]=1, else LABEL[x,1]=1
|
||||
#### if sample_x's is within 0-17, then LABEL[x,2]=1
|
||||
#### elif sample_x's is within 18-44, then LABEL[x,3]=1
|
||||
#### elif sample_x's is within 45-64, then LABEL[x,4]=1
|
||||
#### elif sample_x's is within 64-, then LABEL[x,5]=1
|
||||
LABEL = np.load('')
|
||||
|
||||
#### training parameters
|
||||
NUM_GPUS = 1
|
||||
BATCHSIZE_PER_GPU = 2000
|
||||
TOTAL_BATCHSIZE = BATCHSIZE_PER_GPU * NUM_GPUS
|
||||
STEPS_PER_EPOCH = int(np.load('ICD9/train.npy').shape[0] / 2000)
|
||||
|
||||
g_structure = [128, 128]
|
||||
d_structure = [854, 256, 128]
|
||||
z_dim = 128
|
||||
|
||||
def _variable_on_cpu(name, shape, initializer=None):
|
||||
with tf.device('/cpu:0'):
|
||||
var = tf.get_variable(name, shape, initializer=initializer)
|
||||
return var
|
||||
|
||||
|
||||
def batchnorm(inputs, name, labels=None, n_labels=None):
|
||||
mean, var = tf.nn.moments(inputs, [0], keep_dims=True)
|
||||
shape = mean.shape[1].value
|
||||
offset_m = _variable_on_cpu(shape=[n_labels,shape], name='offset'+name,
|
||||
initializer=tf.zeros_initializer)
|
||||
scale_m = _variable_on_cpu(shape=[n_labels,shape], name='scale'+name,
|
||||
initializer=tf.ones_initializer)
|
||||
offset = tf.nn.embedding_lookup(offset_m, labels)
|
||||
scale = tf.nn.embedding_lookup(scale_m, labels)
|
||||
result = tf.nn.batch_normalization(inputs, mean, var, offset, scale, 1e-8)
|
||||
return result
|
||||
|
||||
|
||||
def layernorm(inputs, name, labels=None, n_labels=None):
|
||||
mean, var = tf.nn.moments(inputs, [1], keep_dims=True)
|
||||
shape = inputs.shape[1].value
|
||||
offset_m = _variable_on_cpu(shape=[n_labels,shape], name='offset'+name,
|
||||
initializer=tf.zeros_initializer)
|
||||
scale_m = _variable_on_cpu(shape=[n_labels,shape], name='scale'+name,
|
||||
initializer=tf.ones_initializer)
|
||||
offset = tf.nn.embedding_lookup(offset_m, labels)
|
||||
scale = tf.nn.embedding_lookup(scale_m, labels)
|
||||
result = tf.nn.batch_normalization(inputs, mean, var, offset, scale, 1e-8)
|
||||
return result
|
||||
|
||||
|
||||
def input_fn():
|
||||
features_placeholder = tf.placeholder(shape=REAL.shape, dtype=tf.float32)
|
||||
labels_placeholder = tf.placeholder(shape=LABEL.shape, dtype=tf.float32)
|
||||
dataset = tf.data.Dataset.from_tensor_slices((features_placeholder, labels_placeholder))
|
||||
dataset = dataset.repeat(10000)
|
||||
dataset = dataset.batch(batch_size=BATCHSIZE_PER_GPU)
|
||||
dataset = dataset.prefetch(1)
|
||||
iterator = dataset.make_initializable_iterator()
|
||||
# next_element = iterator.get_next()
|
||||
# init_op = iterator.initializer
|
||||
return iterator, features_placeholder, labels_placeholder
|
||||
|
||||
|
||||
def generator(z, label):
|
||||
x = z
|
||||
tmp_dim = z_dim
|
||||
with tf.variable_scope('G', reuse=tf.AUTO_REUSE, regularizer=l2_regularizer(0.00001)):
|
||||
for i, dim in enumerate(g_structure[:-1]):
|
||||
kernel = _variable_on_cpu('W_' + str(i), shape=[tmp_dim, dim])
|
||||
h1 = batchnorm(tf.matmul(x, kernel), name='cbn' + str(i), labels=label, n_labels=8)
|
||||
h2 = tf.nn.relu(h1)
|
||||
x = x + h2
|
||||
tmp_dim = dim
|
||||
i = len(g_structure) - 1
|
||||
kernel = _variable_on_cpu('W_' + str(i), shape=[tmp_dim, g_structure[-1]])
|
||||
h1 = batchnorm(tf.matmul(x, kernel), name='cbn' + str(i),
|
||||
labels=label, n_labels=8)
|
||||
h2 = tf.nn.tanh(h1)
|
||||
x = x + h2
|
||||
|
||||
kernel = _variable_on_cpu('W_' + str(i+1), shape=[128, 854])
|
||||
bias = _variable_on_cpu('b_' + str(i+1), shape=[854])
|
||||
x = tf.nn.sigmoid(tf.add(tf.matmul(x, kernel), bias))
|
||||
return x
|
||||
|
||||
|
||||
def discriminator(x, label):
|
||||
with tf.variable_scope('D', reuse=tf.AUTO_REUSE, regularizer=l2_regularizer(0.00001)):
|
||||
for i, dim in enumerate(d_structure[1:]):
|
||||
kernel = _variable_on_cpu('W_' + str(i), shape=[d_structure[i], dim])
|
||||
bias = _variable_on_cpu('b_' + str(i), shape=[dim])
|
||||
x = tf.nn.relu(tf.add(tf.matmul(x, kernel), bias))
|
||||
x = layernorm(x, name='cln' + str(i), labels=label, n_labels=8)
|
||||
i = len(d_structure)
|
||||
kernel = _variable_on_cpu('W_' + str(i), shape=[d_structure[-1], 1])
|
||||
bias = _variable_on_cpu('b_' + str(i), shape=[1])
|
||||
y = tf.add(tf.matmul(x, kernel), bias)
|
||||
return y
|
||||
|
||||
|
||||
def compute_dloss(real, fake, label):
|
||||
epsilon = tf.random_uniform(
|
||||
shape=[BATCHSIZE_PER_GPU, 1],
|
||||
minval=0.,
|
||||
maxval=1.)
|
||||
x_hat = real + epsilon * (fake - real)
|
||||
y_hat_fake = discriminator(fake, label)
|
||||
y_hat_real = discriminator(real, label)
|
||||
y_hat = discriminator(x_hat, label)
|
||||
|
||||
grad = tf.gradients(y_hat, [x_hat])[0]
|
||||
slopes = tf.sqrt(tf.reduce_sum(tf.square(grad), 1))
|
||||
gradient_penalty = tf.reduce_mean((slopes - 1.) ** 2)
|
||||
all_regs = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
|
||||
w_distance = -tf.reduce_mean(y_hat_real) + tf.reduce_mean(y_hat_fake)
|
||||
loss = w_distance + 10 * gradient_penalty + sum(all_regs)
|
||||
tf.add_to_collection('dlosses', loss)
|
||||
|
||||
return w_distance, loss
|
||||
|
||||
|
||||
def compute_gloss(fake, label):
|
||||
y_hat_fake = discriminator(fake, label)
|
||||
all_regs = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
|
||||
loss = -tf.reduce_mean(y_hat_fake) + sum(all_regs)
|
||||
tf.add_to_collection('glosses', loss)
|
||||
return loss, loss
|
||||
|
||||
|
||||
def tower_loss(scope, stage, real, label):
|
||||
label = tf.cast(label, tf.int32)
|
||||
label = label[:, 1] * 4 + tf.squeeze(
|
||||
tf.matmul(label[:, 2:], tf.constant([[0], [1], [2], [3]], dtype=tf.int32)))
|
||||
z = tf.random_normal(shape=[BATCHSIZE_PER_GPU, z_dim])
|
||||
fake = generator(z, label)
|
||||
if stage == 'D':
|
||||
w, loss = compute_dloss(real, fake, label)
|
||||
losses = tf.get_collection('dlosses', scope)
|
||||
else:
|
||||
w, loss = compute_gloss(fake, label)
|
||||
losses = tf.get_collection('glosses', scope)
|
||||
|
||||
total_loss = tf.add_n(losses, name='total_loss')
|
||||
|
||||
# loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg')
|
||||
# loss_averages_op = loss_averages.apply(losses + [total_loss])
|
||||
#
|
||||
# with tf.control_dependencies([loss_averages_op]):
|
||||
# total_loss = tf.identity(total_loss)
|
||||
|
||||
return total_loss, w
|
||||
|
||||
|
||||
def average_gradients(tower_grads):
|
||||
average_grads = []
|
||||
for grad_and_vars in zip(*tower_grads):
|
||||
grads = []
|
||||
for g, _ in grad_and_vars:
|
||||
expanded_g = tf.expand_dims(g, 0)
|
||||
grads.append(expanded_g)
|
||||
|
||||
grad = tf.concat(axis=0, values=grads)
|
||||
grad = tf.reduce_mean(grad, 0)
|
||||
|
||||
v = grad_and_vars[0][1]
|
||||
grad_and_var = (grad, v)
|
||||
average_grads.append(grad_and_var)
|
||||
return average_grads
|
||||
|
||||
|
||||
def graph(stage, opt):
|
||||
# global_step = tf.get_variable(stage+'_step', [], initializer=tf.constant_initializer(0), trainable=False)
|
||||
tower_grads = []
|
||||
per_gpu_w = []
|
||||
iterator, features_placeholder, labels_placeholder = input_fn()
|
||||
with tf.variable_scope(tf.get_variable_scope()):
|
||||
for i in range(NUM_GPUS):
|
||||
with tf.device('/gpu:%d' % i):
|
||||
with tf.name_scope('%s_%d' % ('TOWER', i)) as scope:
|
||||
(real, label) = iterator.get_next()
|
||||
loss, w = tower_loss(scope, stage, real, label)
|
||||
tf.get_variable_scope().reuse_variables()
|
||||
vars_ = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=stage)
|
||||
grads = opt.compute_gradients(loss, vars_)
|
||||
tower_grads.append(grads)
|
||||
per_gpu_w.append(w)
|
||||
|
||||
grads = average_gradients(tower_grads)
|
||||
apply_gradient_op = opt.apply_gradients(grads)
|
||||
|
||||
mean_w = tf.reduce_mean(per_gpu_w)
|
||||
train_op = apply_gradient_op
|
||||
return train_op, mean_w, iterator, features_placeholder, labels_placeholder
|
||||
|
||||
|
||||
def train(max_epochs, train_dir):
|
||||
with tf.device('/cpu:0'):
|
||||
opt_d = tf.train.AdamOptimizer(1e-4)
|
||||
opt_g = tf.train.AdamOptimizer(1e-4)
|
||||
train_d, w_distance, iterator_d, features_placeholder_d, labels_placeholder_d = graph('D', opt_d)
|
||||
train_g, _, iterator_g, features_placeholder_g, labels_placeholder_g = graph('G', opt_g)
|
||||
saver = tf.train.Saver()
|
||||
init = tf.global_variables_initializer()
|
||||
|
||||
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)) as sess:
|
||||
sess.run(init)
|
||||
sess.run(iterator_d.initializer,
|
||||
feed_dict={features_placeholder_d: REAL, labels_placeholder_d: LABEL})
|
||||
sess.run(iterator_g.initializer,
|
||||
feed_dict={features_placeholder_g: REAL, labels_placeholder_g: LABEL})
|
||||
|
||||
for epoch in range(1, max_epochs + 1):
|
||||
start_time = time.time()
|
||||
w_sum = 0
|
||||
for i in range(STEPS_PER_EPOCH):
|
||||
for _ in range(2):
|
||||
_, w = sess.run([train_d, w_distance])
|
||||
w_sum += w
|
||||
sess.run(train_g)
|
||||
duration = time.time() - start_time
|
||||
|
||||
assert not np.isnan(w_sum), 'Model diverged with loss = NaN'
|
||||
|
||||
format_str = 'epoch: %d, w_distance = %f (%.1f)'
|
||||
print(format_str % (epoch, -w_sum/(STEPS_PER_EPOCH*2), duration))
|
||||
if epoch % 500 == 0:
|
||||
# checkpoint_path = os.path.join(train_dir, 'multi')
|
||||
saver.save(sess, train_dir, write_meta_graph=False, global_step=epoch)
|
||||
# saver.save(sess, train_dir, global_step=epoch)
|
||||
|
||||
|
||||
def generate(model_dir, synthetic_dir, demo):
|
||||
tf.reset_default_graph()
|
||||
z = tf.random_normal(shape=[BATCHSIZE_PER_GPU, z_dim])
|
||||
y = tf.placeholder(shape=[BATCHSIZE_PER_GPU, 6], dtype=tf.int32)
|
||||
label = y[:, 1] * 4 + tf.squeeze(tf.matmul(y[:, 2:], tf.constant([[0], [1], [2], [3]], dtype=tf.int32)))
|
||||
fake = generator(z, label)
|
||||
saver = tf.train.Saver()
|
||||
with tf.Session() as sess:
|
||||
saver.restore(sess, model_dir)
|
||||
for m in range(2):
|
||||
for n in range(2, 6):
|
||||
idx1 = (demo[:, m] == 1)
|
||||
idx2 = (demo[:, n] == 1)
|
||||
idx = [idx1[j] and idx2[j] for j in range(len(idx1))]
|
||||
num = np.sum(idx)
|
||||
nbatch = int(np.ceil(num / BATCHSIZE_PER_GPU))
|
||||
label_input = np.zeros((nbatch*BATCHSIZE_PER_GPU, 6))
|
||||
label_input[:, n] = 1
|
||||
label_input[:, m] = 1
|
||||
output = []
|
||||
for i in range(nbatch):
|
||||
f = sess.run(fake,feed_dict={y: label_input[i*BATCHSIZE_PER_GPU:(i+1)*BATCHSIZE_PER_GPU]})
|
||||
output.extend(np.round(f))
|
||||
output = np.array(output)[:num]
|
||||
np.save(synthetic_dir + str(m) + str(n), output)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
#### args_1: number of training epochs
|
||||
#### args_2: dir to save the trained model
|
||||
train(500, '')
|
||||
|
||||
#### args_1: dir of trained model
|
||||
#### args_2: dir to save synthetic data
|
||||
#### args_3, label of data-to-be-generated
|
||||
generate('', '', demo=LABEL)
|
||||
|
@ -1,12 +0,0 @@
|
||||
{
|
||||
"type": "service_account",
|
||||
"project_id": "aou-res-deid-vumc-test",
|
||||
"private_key_id": "8b7acef9a1f1137799011cf13cf0906e331c472e",
|
||||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCYRPv0ZMGLXjva\nVZjJlcApDpXhJl2iDghhG0JqUH1PmuLjMtmhuMSgweq+M3KNF92Wft9Ree+fTN6m\nVtyqZMgz1qXi6I1WJHyT+ndtk4eWlE4O1AxE0QkfLqtj1kafU6Yu2tGpZ23jHFG9\nc7oq1tqPwC39pKE3ScShcpbZxFqvOFwW7ZSHEQ2Zk0/9lA0bfQH+Vaq1JqBbMkCO\nh1p1ptXPHyIoTjgbtQ/3N6JHA9XpqF1DHFQTe6H/4Zc+GUBV8kb/9pdeybcrhd1K\nVzuT6pAkOLQ7Wtq9Hwl3zAF3jyhlEpirYt4tjcw1pq0phhUuDGcLS37cTzWkqekr\nFEp8NkSnAgMBAAECggEAI16Kw+cPigb2ki2l0tVlEGRh7i2SPE1UJvJFCBrwMKiC\noVGzebxIeCrzEwEyT5HGl+mah/tx7KfXY/3zPeUxF9F5MO7hvau2AE2CpkJJkXGb\nfBhHTUjc/JBDoWopd2LfzCxp3Ra4ULPITOBv0vmbRR7Xz/4IsKYC9Zl/btAMXHy4\nJZZuifK8mCD4BDXxG6W2p+jqeKFjKYTuHyCKWy9u8NnnH6eoNMLvewr/P3pPZK9l\nSFQDV0nWU0yZoR4cccYHtq/9Uw1pY7A9iNYI4JnAnPam8Rka0OEgZbqMVsk3FUmA\nG+SOtiJ9iopQsW5g/HTG7Q420gijnfe5IWQK6yLBOQKBgQDNCuGexHMUGB+/bxFK\nnQ+AiktFib76PbMYFSGdsQQYHGcNHXmXRnJbpj/llO7tiWk/akOA0UrjtipXERTP\nYoXRDlghvnluxUYDm+mD94jSe7rE45b+sNH8FyqgrHWJVHSPBcIz0YXCUxRmE9eq\n4BcNfTqtjAl7hasWhGUVlXppawKBgQC+HJn1Lpvp89h+7ge09p6SU6RhAbOygrtA\nBD3Odr6WV6SGXEKyFHSHLkRVA1BFzzTXl3nEJvHFe7I5RNnVzWSqmf4LkBcIDqQO\nmiNb2TbA/h4utlMJvTrit03qdzngvgmoWyKqNpxmj6afNU/up4ck0hqBkJae/FBQ\nkoSwXcA0tQKBgDJzE/JZiasPCHi0nj+Kh27sF/sjGj8+ARvSzzOag1RfYKekce9b\noPWV4TDexS7i2WeGANfoJxICF0bW6BTiu+QlMGAVGpG7ri9jJECZHiwTz290RAmk\nffYVySJBbKX+hrNOCmtviQa4JFO9XBoqCuIBxvc+dnLS/7aJmsmFvtnDAoGAfQRf\n9gzdeN7i+q1bIhSfuIgKa8RrwDMaIgHoBxKtSD6AMd8P+P1cl9zEEMeqDQ4yqKey\n6lvV19D9JY3yVhfIYCv+FOp/Sswd9IBGSkswJ3+0p3E8cAYhaB+0vEAFLpap0S2F\nQTvCY+uJXd74Hm/KflswFQ3ZDtnLkwCXA0fTcpUCgYBMkcE6Bn0tIShaXsaaufIW\nXrJ6gtEUDtUXP85lNO7hUxBWTu2dF6OsgBniNfWypmRecaZsFl/sD6YKT0bV1vvv\nU0uhYTDx5z7o8ahvjBwOqF5sDDVX02umFBoG16zd3hpOJrGSh+ESpJhWw5dV6m5J\n530zPFObyt2kI9+E75+G/w==\n-----END PRIVATE KEY-----\n",
|
||||
"client_email": "dev-deid-600@aou-res-deid-vumc-test.iam.gserviceaccount.com",
|
||||
"client_id": "104228831510203920964",
|
||||
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
||||
"token_uri": "https://oauth2.googleapis.com/token",
|
||||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
||||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dev-deid-600%40aou-res-deid-vumc-test.iam.gserviceaccount.com"
|
||||
}
|
Loading…
Reference in new issue