Python 3.x I';我在跟踪一个tensorflow程序,但是那里的代码被破坏了,我可以';我想不出怎么修理它

Python 3.x I';我在跟踪一个tensorflow程序,但是那里的代码被破坏了,我可以';我想不出怎么修理它,python-3.x,tensorflow,Python 3.x,Tensorflow,我需要一个for循环来定义一个变量,这个变量非常重要,但它不会运行(没有错误,就是不会运行)。无论我尝试了什么,循环似乎都不会运行,没有它,我无法继续项目 我尝试过改变循环,但它没有给我任何东西 from __future__ import absolute_import, division, print_function, unicode_literals import tensorflow as tf tf.enable_eager_execution() import numpy as n

我需要一个for循环来定义一个变量,这个变量非常重要,但它不会运行(没有错误,就是不会运行)。无论我尝试了什么,循环似乎都不会运行,没有它,我无法继续项目

我尝试过改变循环,但它没有给我任何东西

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
tf.enable_eager_execution()
import numpy as np
import os
import time

path = '/home/student/Desktop/Carson.M/tina/sample.txt'  # when editing file MUST save over original or won't change

# Read, then decode for py2 compat.

text = open(path, 'rb').read().decode(encoding='utf-8')
# length of text is the number of characters in it
print('Length of text: {} characters'.format(len(text)))
# print the first 250 characters
print(text[:250])
# count unique characters
vocab = sorted(set(text))
print('{} unique characters'.format(len(vocab)))
# mapping from unique characters to indices
char2idx = {u: i for i, u in enumerate(vocab)}
idx2char = np.array(vocab)

text_as_int = np.array([char2idx[c] for c in text])
print('{')
for char, _ in zip(char2idx, range(20)):
    print('  {:4s}: {:3d},'.format(repr(char), char2idx[char]))
print('  ...\n}')
# Show how the first 13 characters from the text are mapped to integers
print('{} ---- characters mapped to int -----> {}'.format(repr(text[:13]), text_as_int[:13]))

# The maximum length sentence we want for a single input in characters
seq_length = 100
examples_per_epoch = len(text)//seq_length

# Create training examples / targets
char_dataset = tf.data.Dataset.from_tensor_slices(text_as_int)

for i in char_dataset.take(5):
    print(idx2char[i.numpy()])

sequences = char_dataset.batch(seq_length+1, drop_remainder=True)

for item in sequences.take(5):
    print(repr(''.join(idx2char[item.numpy()])))


def split_input_target(chunk):
    input_text = chunk[:-1]
    target_text = chunk[1:]
    return input_text, target_text


dataset = sequences.map(split_input_target)
for input_example, target_example in dataset.take(1):
    print('Input data: ', repr(''.join(idx2char[input_example.numpy()])))
    print('Target Dat: ', repr(''.join(idx2char[target_example.numpy()])))
    # print('this loop worked')

# for input_example, target_example in dataset.take(1):
#     print('Input data: ', repr(''.join(idx2char[input_example.numpy()])))
#     print('Target data:', repr(''.join(idx2char[target_example.numpy()])))


for i, (input_idx, target_idx) in enumerate(zip(input_example[:5], target_example[:5])):
    print("Step {:4d}".format(i))
    print('     input: {} ({:s})'.format(input_idx, repr(idx2char[target_idx])))
    print('     expected output: {} ({:s})'.format(input_idx, repr(idx2char[target_idx])))


# for i, (input_idx, target_idx) in enumerate(zip(input_example[:5], target_example[:5])):
#     print("Step {:4d}".format(i))
#     print("  input: {} ({:s})".format(input_idx, repr(idx2char[input_idx])))
#     print("  expected output: {} ({:s})".format(target_idx, repr(idx2char[target_idx])))

# Batch size
BATCH_SIZE = 64
steps_per_epoch = examples_per_epoch//BATCH_SIZE
# Buffer size to shuffle the dataset
# (TF data is designed to work with possibly infinite sequences,
# so it doesn't attempt to shuffle the entire sequence in memory. Instead,
# it maintains a buffer in which it shuffles elements).
BUFFER_SIZE = 10000

dataset = dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)


# Length of the vocabulary in chars
vocab_size = len(vocab)
# The embedding dimension
embedding_dim = 256
# Number of RNN units
rnn_units = 1024
if tf.test.is_gpu_available():
    rnn = tf.keras.layers.CuDNNGRU
else:
    import functools
    rnn = functools.partial(tf.keras.layers.GRU, recurrent_activation='sigmoid')


# def build_model(vocab_size, embedding_dim, rnn_units, batch_size):
#     model = tf.keras.Sequential([tf.keras.layers.Embedding(vocab_size,
#                                                            embedding_dim, batch_input_shape=[batch_size, None])
#                                 , rnn(rnn_units, return_sequences=True, recurrent_initializer='glorot_uniform'
#                                 , stateful=True), tf.keras.layers.Dense(vocab_size)])
#     return model


def build_model(vocab_size, embedding_dim, rnn_units, batch_size):
    model = tf.keras.Sequential([
        tf.keras.layers.Embedding(vocab_size, embedding_dim, batch_input_shape=[batch_size, None]),
        rnn(rnn_units,
            return_sequences=True,
            recurrent_initializer='glorot_uniform',
            stateful=True),
        tf.keras.layers.Dense(vocab_size)
  ])
    return model


model = build_model(
  vocab_size=len(vocab),
  embedding_dim=embedding_dim,
  rnn_units=rnn_units,
  batch_size=BATCH_SIZE)

# code skips for some reason
# print(dataset)

for input_example_batch, target_example_batch in dataset.take(1):
    example_batch_predictions = model(input_example_batch)
    print(example_batch_predictions.shape, "# (batch_size, sequence_length, vocab_size)")
    print('it works')


# model = build_model(vocab_size=len(vocab), embedding_dim=embedding_dim, rnn_units=rnn_units, batch_size=BATCH_SIZE)
#
# model.summary()
# sampled_indices = tf.random.categorical(example_batch_predictions[0], num_samples=1)
# sampled_indices = tf.random.categorical(example_batch_predictions[0], num_samples=1)
# sampled_indices = tf.squeeze(sampled_indices, axis=1).numpy()
# sampled_indices

# unfinished

它应该在底部打印(64、100、65)35;(批次大小、序列长度、声音大小)(或稍有不同的数字)

您注释掉了
打印(数据集)
语句。包含时您看到了什么?print语句是为了测试它打印的数据集变量“”中的内容。如果for循环根本没有执行,那么第一个也是最明显的猜测是
数据集中没有任何内容。由于
.take
返回另一个包含1个元素的数据集,因此
for
应该只有1个元素可以循环,但如果存在元素,则至少应该得到一次迭代。你能试着打印
len(dataset)
?它会出现错误,类型为“BatchDataset”的对象没有len()OK,因为我刚刚发现数据集是生成器,没有长度。我不知道在TF V1中获得数据集长度的简单方法,除了使用
len([I for I in dataset])
这种方法不符合目的,因为它依赖于列表理解中的
for
循环,但我打赌
dataset.take(1)
没有元素。您没有得到异常这一事实意味着for循环中的代码正在工作,但没有可从
数据集检索的元素。取(1)
,因此它什么也不做。您注释掉了
print(dataset)
语句。包含时您看到了什么?print语句是为了测试它打印的数据集变量“”中的内容。如果for循环根本没有执行,那么第一个也是最明显的猜测是
数据集中没有任何内容。由于
.take
返回另一个包含1个元素的数据集,因此
for
应该只有1个元素可以循环,但如果存在元素,则至少应该得到一次迭代。你能试着打印
len(dataset)
?它会出现错误,类型为“BatchDataset”的对象没有len()OK,因为我刚刚发现数据集是生成器,没有长度。我不知道在TF V1中获得数据集长度的简单方法,除了使用
len([I for I in dataset])
这种方法不符合目的,因为它依赖于列表理解中的
for
循环,但我打赌
dataset.take(1)
没有元素。您没有得到异常这一事实意味着for循环中的代码正在工作,但没有可从
数据集检索的元素。取(1)
,因此它什么也不做。