本文整理汇总了Python中train.Graph方法的典型用法代码示例。如果您正苦于以下问题:Python train.Graph方法的具体用法?Python train.Graph怎么用?Python train.Graph使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类train
的用法示例。
在下文中一共展示了train.Graph方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: synthesize
# 需要导入模块: import train [as 别名]
# 或者: from train import Graph [as 别名]
def synthesize():
if not os.path.exists(hp.sampledir): os.mkdir(hp.sampledir)
# Load graph
g = Graph(mode="synthesize"); print("Graph loaded")
# Load data
texts = load_data(mode="synthesize")
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, tf.train.latest_checkpoint(hp.logdir)); print("Restored!")
# Feed Forward
## mel
y_hat = np.zeros((texts.shape[0], 200, hp.n_mels*hp.r), np.float32) # hp.n_mels*hp.r
for j in tqdm.tqdm(range(200)):
_y_hat = sess.run(g.y_hat, {g.x: texts, g.y: y_hat})
y_hat[:, j, :] = _y_hat[:, j, :]
## mag
mags = sess.run(g.z_hat, {g.y_hat: y_hat})
for i, mag in enumerate(mags):
print("File {}.wav is being generated ...".format(i+1))
audio = spectrogram2wav(mag)
write(os.path.join(hp.sampledir, '{}.wav'.format(i+1)), hp.sr, audio)
示例2: test
# 需要导入模块: import train [as 别名]
# 或者: from train import Graph [as 别名]
def test():
x, y = load_data(type="test")
g = Graph(is_training=False)
with g.graph.as_default():
sv = tf.train.Supervisor()
with sv.managed_session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
# Restore parameters
sv.saver.restore(sess, tf.train.latest_checkpoint(hp.logdir))
print("Restored!")
# Get model name
mname = open(hp.logdir + '/checkpoint', 'r').read().split('"')[1] # model name
if not os.path.exists('results'): os.mkdir('results')
fout = 'results/{}.txt'.format(mname)
import copy
_preds = copy.copy(x)
while 1:
istarget, probs, preds = sess.run([g.istarget, g.probs, g.preds], {g.x:_preds, g.y: y})
probs = probs.astype(np.float32)
preds = preds.astype(np.float32)
probs *= istarget #(N, 9, 9)
preds *= istarget #(N, 9, 9)
probs = np.reshape(probs, (-1, 9*9)) #(N, 9*9)
preds = np.reshape(preds, (-1, 9*9))#(N, 9*9)
_preds = np.reshape(_preds, (-1, 9*9))
maxprob_ids = np.argmax(probs, axis=1) # (N, ) <- blanks of the most probable prediction
maxprobs = np.max(probs, axis=1, keepdims=False)
for j, (maxprob_id, maxprob) in enumerate(zip(maxprob_ids, maxprobs)):
if maxprob != 0:
_preds[j, maxprob_id] = preds[j, maxprob_id]
_preds = np.reshape(_preds, (-1, 9, 9))
_preds = np.where(x==0, _preds, y) # # Fill in the non-blanks with correct numbers
if np.count_nonzero(_preds) == _preds.size: break
write_to_file(x.astype(np.int32), y, _preds.astype(np.int32), fout)
示例3: test
# 需要导入模块: import train [as 别名]
# 或者: from train import Graph [as 别名]
def test():
# Load data: two samples
files, speaker_ids = load_data(mode="test")
speaker_ids = speaker_ids[::-1] # swap
# Parse
x = np.zeros((2, 63488, 1), np.int32)
for i, f in enumerate(files):
f = np.load(f)
length = min(63488, len(f))
x[i, :length, :] = f[:length]
# Graph
g = Graph("test"); print("Test Graph loaded")
with tf.Session() as sess:
saver = tf.train.Saver()
# Restore saved variables
ckpt = tf.train.latest_checkpoint(hp.logdir)
if ckpt is not None: saver.restore(sess, ckpt)
# Feed Forward
y_hat = np.zeros((2, 63488, 1), np.int32)
for j in tqdm(range(63488)):
_y_hat = sess.run(g.y_hat, {g.x: x, g.y: y_hat, g.speaker_ids: speaker_ids})
_y_hat = np.expand_dims(_y_hat, -1)
y_hat[:, j, :] = _y_hat[:, j, :]
for i, y in tqdm(enumerate(y_hat)):
audio = mu_law_decode(y)
write(os.path.join(hp.sampledir, '{}.wav'.format(i + 1)), hp.sr, audio)
示例4: eval
# 需要导入模块: import train [as 别名]
# 或者: from train import Graph [as 别名]
def eval():
# Load graph
g = Graph(is_training=False); print("Graph loaded")
# Load data
x, y = load_eval_data()
char2idx, idx2char = load_vocab()
with g.graph.as_default():
sv = tf.train.Supervisor()
with sv.managed_session() as sess:
# Restore parameters
sv.saver.restore(sess, tf.train.latest_checkpoint(hp.logdir))
print("Restored!")
# Get model name
mname = open(hp.logdir + '/checkpoint', 'r').read().split('"')[1]
# Speech to Text
if not os.path.exists('samples'): os.mkdir('samples')
with codecs.open('samples/{}.txt'.format(mname), 'w', 'utf-8') as fout:
preds = np.zeros((hp.batch_size, hp.max_len), np.int32)
for j in range(hp.max_len):
_preds = sess.run(g.preds, {g.x: x, g.y: preds})
preds[:, j] = _preds[:, j]
# Write to file
for i, (expected, got) in enumerate(zip(y, preds)): # ground truth vs. prediction
fout.write("Expected: {}\n".format(expected.split("S")[0]))
fout.write("Got : {}\n\n".format(("".join(idx2char[idx] for idx in np.fromstring(got, np.int32))).split("S")[0]))
fout.flush()
示例5: eval
# 需要导入模块: import train [as 别名]
# 或者: from train import Graph [as 别名]
def eval():
# Load graph
g = Graph(mode="eval"); print("Evaluation Graph loaded")
# Load data
fpaths, text_lengths, texts = load_data(mode="eval")
# Parse
text = np.fromstring(texts[0], np.int32) # (None,)
fname, mel, mag = load_spectrograms(fpaths[0])
x = np.expand_dims(text, 0) # (1, None)
y = np.expand_dims(mel, 0) # (1, None, n_mels*r)
z = np.expand_dims(mag, 0) # (1, None, n_mfccs)
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, tf.train.latest_checkpoint(hp.logdir)); print("Restored!")
writer = tf.summary.FileWriter(hp.logdir, sess.graph)
# Feed Forward
## mel
y_hat = np.zeros((1, y.shape[1], y.shape[2]), np.float32) # hp.n_mels*hp.r
for j in range(y.shape[1]):
_y_hat = sess.run(g.y_hat, {g.x: x, g.y: y_hat})
y_hat[:, j, :] = _y_hat[:, j, :]
## mag
merged, gs = sess.run([g.merged, g.global_step], {g.x:x, g.y:y, g.y_hat: y_hat, g.z: z})
writer.add_summary(merged, global_step=gs)
writer.close()
示例6: synthesize
# 需要导入模块: import train [as 别名]
# 或者: from train import Graph [as 别名]
def synthesize():
# Load data
L = load_data("synthesize")
# Load graph
g = Graph(mode="synthesize"); print("Graph loaded")
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Restore parameters
var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'Text2Mel')
saver1 = tf.train.Saver(var_list=var_list)
saver1.restore(sess, tf.train.latest_checkpoint(hp.logdir + "-1"))
print("Text2Mel Restored!")
var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'SSRN') + \
tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, 'gs')
saver2 = tf.train.Saver(var_list=var_list)
saver2.restore(sess, tf.train.latest_checkpoint(hp.logdir + "-2"))
print("SSRN Restored!")
# Feed Forward
## mel
Y = np.zeros((len(L), hp.max_T, hp.n_mels), np.float32)
prev_max_attentions = np.zeros((len(L),), np.int32)
for j in tqdm(range(hp.max_T)):
_gs, _Y, _max_attentions, _alignments = \
sess.run([g.global_step, g.Y, g.max_attentions, g.alignments],
{g.L: L,
g.mels: Y,
g.prev_max_attentions: prev_max_attentions})
Y[:, j, :] = _Y[:, j, :]
prev_max_attentions = _max_attentions[:, j]
# Get magnitude
Z = sess.run(g.Z, {g.Y: Y})
# Generate wav files
if not os.path.exists(hp.sampledir): os.makedirs(hp.sampledir)
for i, mag in enumerate(Z):
print("Working on file", i+1)
wav = spectrogram2wav(mag)
write(hp.sampledir + "/{}.wav".format(i+1), hp.sr, wav)
示例7: eval
# 需要导入模块: import train [as 别名]
# 或者: from train import Graph [as 别名]
def eval():
# Load graph
g = Graph(is_training=False)
print("Graph loaded")
# Load data
X, Sources, Targets = load_test_data()
de2idx, idx2de = load_de_vocab()
en2idx, idx2en = load_en_vocab()
# X, Sources, Targets = X[:33], Sources[:33], Targets[:33]
# Start session
with g.graph.as_default():
sv = tf.train.Supervisor()
with sv.managed_session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
## Restore parameters
sv.saver.restore(sess, tf.train.latest_checkpoint(hp.logdir))
print("Restored!")
## Get model name
mname = open(hp.logdir + '/checkpoint', 'r').read().split('"')[1] # model name
## Inference
if not os.path.exists('results'): os.mkdir('results')
with codecs.open("results/" + mname, "w", "utf-8") as fout:
list_of_refs, hypotheses = [], []
for i in range(len(X) // hp.batch_size):
### Get mini-batches
x = X[i*hp.batch_size: (i+1)*hp.batch_size]
sources = Sources[i*hp.batch_size: (i+1)*hp.batch_size]
targets = Targets[i*hp.batch_size: (i+1)*hp.batch_size]
### Autoregressive inference
preds = np.zeros((hp.batch_size, hp.maxlen), np.int32)
for j in range(hp.maxlen):
_preds = sess.run(g.preds, {g.x: x, g.y: preds})
preds[:, j] = _preds[:, j]
### Write to file
for source, target, pred in zip(sources, targets, preds): # sentence-wise
got = " ".join(idx2en[idx] for idx in pred).split("</S>")[0].strip()
fout.write("- source: " + source +"\n")
fout.write("- expected: " + target + "\n")
fout.write("- got: " + got + "\n\n")
fout.flush()
# bleu score
ref = target.split()
hypothesis = got.split()
if len(ref) > 3 and len(hypothesis) > 3:
list_of_refs.append([ref])
hypotheses.append(hypothesis)
## Calculate bleu score
score = corpus_bleu(list_of_refs, hypotheses)
fout.write("Bleu Score = " + str(100*score))
示例8: eval
# 需要导入模块: import train [as 别名]
# 或者: from train import Graph [as 别名]
def eval():
# Load graph
g = Graph(mode="test")
print("Graph loaded")
# Load batch
_Y = load_data(mode="test")
X = np.zeros((len(_Y), hp.maxlen))
Y = np.zeros((len(_Y), hp.maxlen))
for i, y in enumerate(_Y):
y = np.fromstring(y, np.int32)
Y[i][:len(y)] = y
np.random.shuffle(y)
X[i][:len(y)] = y
word2idx, idx2word = g.word2idx, g.idx2word
# Start session
with g.graph.as_default():
sv = tf.train.Supervisor()
with sv.managed_session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
# Restore parameters
sv.saver.restore(sess, tf.train.latest_checkpoint(hp.logdir))
# Get model
mname = open(hp.logdir + '/checkpoint', 'r').read().split('"')[1] # model name
# inference
if not os.path.exists('results'): os.mkdir('results')
with codecs.open("results/" + mname, "w", "utf-8") as fout:
num_words, total_edit_distance = 0, 0
for i in range(0, len(Y), hp.batch_size):
### Get mini-batches
x = X[i:i+hp.batch_size]
y = Y[i:i+hp.batch_size]
### Autoregressive inference
preds = np.zeros((hp.batch_size, hp.maxlen), np.int32)
for j in range(hp.maxlen):
_preds = sess.run(g.preds, {g.x: x, g.y: preds})
preds[:, j] = _preds[:, j]
for xx, yy, pred in zip(x, y, preds): # sentence-wise
inputs = " ".join(idx2word[idx] for idx in xx).replace("_", "").strip()
expected = " ".join(idx2word[idx] for idx in yy).replace("_", "").strip()
got = " ".join(idx2word[idx] for idx in pred[:len(inputs.split())])
edit_distance = distance.levenshtein(expected.split(), got.split())
total_edit_distance += edit_distance
num_words += len(expected.split())
fout.write(u"Inputs : {}\n".format(inputs))
fout.write(u"Expected: {}\n".format(expected))
fout.write(u"Got : {}\n".format(got))
fout.write(u"WER : {}\n\n".format(edit_distance))
fout.write(u"Total WER: {}/{}={}\n".format(total_edit_distance,
num_words,
round(float(total_edit_distance) / num_words, 2)))
示例9: eval
# 需要导入模块: import train [as 别名]
# 或者: from train import Graph [as 别名]
def eval():
g = Graph(is_training = False)
print("MSG : Graph loaded!")
X, Sources, Targets = load_data('test')
en2idx, idx2en = load_vocab('en.vocab.tsv')
de2idx, idx2de = load_vocab('de.vocab.tsv')
with g.graph.as_default():
sv = tf.train.Supervisor()
with sv.managed_session(config = tf.ConfigProto(allow_soft_placement = True)) as sess:
# load pre-train model
sv.saver.restore(sess, tf.train.latest_checkpoint(pm.checkpoint))
print("MSG : Restore Model!")
mname = open(pm.checkpoint + '/checkpoint', 'r').read().split('"')[1]
if not os.path.exists('Results'):
os.mkdir('Results')
with codecs.open("Results/" + mname, 'w', 'utf-8') as f:
list_of_refs, predict = [], []
# Get a batch
for i in range(len(X) // pm.batch_size):
x = X[i * pm.batch_size: (i + 1) * pm.batch_size]
sources = Sources[i * pm.batch_size: (i + 1) * pm.batch_size]
targets = Targets[i * pm.batch_size: (i + 1) * pm.batch_size]
# Autoregressive inference
preds = np.zeros((pm.batch_size, pm.maxlen), dtype = np.int32)
for j in range(pm.maxlen):
_preds = sess.run(g.preds, feed_dict = {g.inpt: x, g.outpt: preds})
preds[:, j] = _preds[:, j]
for source, target, pred in zip(sources, targets, preds):
got = " ".join(idx2de[idx] for idx in pred).split("<EOS>")[0].strip()
f.write("- Source: {}\n".format(source))
f.write("- Ground Truth: {}\n".format(target))
f.write("- Predict: {}\n\n".format(got))
f.flush()
# Bleu Score
ref = target.split()
prediction = got.split()
if len(ref) > pm.word_limit_lower and len(prediction) > pm.word_limit_lower:
list_of_refs.append([ref])
predict.append(prediction)
score = corpus_bleu(list_of_refs, predict)
f.write("Bleu Score = " + str(100 * score))
示例10: eval
# 需要导入模块: import train [as 别名]
# 或者: from train import Graph [as 别名]
def eval():
# Load graph
g = Graph(is_training=False)
print("Graph loaded")
# Load data
X, Y = load_data(mode="test") # texts
char2idx, idx2char = load_vocab()
with g.graph.as_default():
sv = tf.train.Supervisor()
with sv.managed_session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
# Restore parameters
sv.saver.restore(sess, tf.train.latest_checkpoint(hp.logdir))
print("Restored!")
# Get model
mname = open(hp.logdir + '/checkpoint', 'r').read().split('"')[1] # model name
# Inference
if not os.path.exists(hp.savedir): os.mkdir(hp.savedir)
with open("{}/{}".format(hp.savedir, mname), 'w') as fout:
results = []
baseline_results = []
for step in range(len(X) // hp.batch_size):
x = X[step * hp.batch_size: (step + 1) * hp.batch_size]
y = Y[step * hp.batch_size: (step + 1) * hp.batch_size]
# predict characters
preds = sess.run(g.preds, {g.x: x})
for xx, yy, pp in zip(x, y, preds): # sentence-wise
expected = ''
got = ''
for xxx, yyy, ppp in zip(xx, yy, pp): # character-wise
if xxx == 0:
break
else:
got += idx2char.get(xxx, "*")
expected += idx2char.get(xxx, "*")
if ppp == 1: got += " "
if yyy == 1: expected += " "
# prediction results
if ppp == yyy:
results.append(1)
else:
results.append(0)
# baseline results
if yyy == 0: # no space
baseline_results.append(1)
else:
baseline_results.append(0)
fout.write("▌Expected: " + expected + "\n")
fout.write("▌Got: " + got + "\n\n")
fout.write(
"Final Accuracy = %d/%d=%.4f\n" % (sum(results), len(results), float(sum(results)) / len(results)))
fout.write(
"Baseline Accuracy = %d/%d=%.4f" % (sum(baseline_results), len(baseline_results), float(sum(baseline_results)) / len(baseline_results)))
示例11: eval
# 需要导入模块: import train [as 别名]
# 或者: from train import Graph [as 别名]
def eval():
# Load graph
g = Graph(mode="inference"); print("Graph Loaded")
with tf.Session() as sess:
# Initialize variables
tf.sg_init(sess)
# Restore parameters
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint('asset/train'))
print("Restored!")
mname = open('asset/train/checkpoint', 'r').read().split('"')[1] # model name
# Load data
X, Sources, Targets = load_test_data()
char2idx, idx2char = load_vocab()
with codecs.open(mname, "w", "utf-8") as fout:
list_of_refs, hypotheses = [], []
for i in range(len(X) // Hp.batch_size):
# Get mini-batches
x = X[i*Hp.batch_size: (i+1)*Hp.batch_size] # mini-batch
sources = Sources[i*Hp.batch_size: (i+1)*Hp.batch_size]
targets = Targets[i*Hp.batch_size: (i+1)*Hp.batch_size]
preds_prev = np.zeros((Hp.batch_size, Hp.maxlen), np.int32)
preds = np.zeros((Hp.batch_size, Hp.maxlen), np.int32)
for j in range(Hp.maxlen):
# predict next character
outs = sess.run(g.preds, {g.x: x, g.y_src: preds_prev})
# update character sequence
if j < Hp.maxlen - 1:
preds_prev[:, j + 1] = outs[:, j]
preds[:, j] = outs[:, j]
# Write to file
for source, target, pred in zip(sources, targets, preds): # sentence-wise
got = "".join(idx2char[idx] for idx in pred).split(u"␃")[0]
fout.write("- source: " + source +"\n")
fout.write("- expected: " + target + "\n")
fout.write("- got: " + got + "\n\n")
fout.flush()
# For bleu score
ref = target.split()
hypothesis = got.split()
if len(ref) > 2:
list_of_refs.append([ref])
hypotheses.append(hypothesis)
# Get bleu score
score = corpus_bleu(list_of_refs, hypotheses)
fout.write("Bleu Score = " + str(100*score))