我试图在TensorFlow中实现一个非常基本的神经网络,但我遇到了一些问题.这是一个非常基本的网络,作为值(小时或睡眠和学习时间)的输入,并预测测试的分数(我在你的管上找到了这个例子).所以基本上我只有一个隐藏层有三个单元,每个单元计算一个激活函数(sigmoid),成本函数是平方误差的总和,我使用梯度下降来最小化它.所以问题是,当我使用训练数据训练网并尝试使用相同的训练数据进行一些预测时,结果不完全匹配,并且它们看起来也很奇怪,因为看起来彼此相等.
import tensorflow as tf
import numpy as np
import input_data
sess = tf.InteractiveSession()
# create a 2-D version of input for plotting
trX = np.matrix(([3,5], [5,1],[10,2]), dtype=float)
trY = np.matrix(([85], [82], [93]), dtype=float) # 3X1 matrix
trX = trX / np.max(trX, axis=0)
trY = trY / 100 # 100 is the maximum score allowed
teX = np.matrix(([3,5]), dtype=float)
teY = np.matrix(([85]), dtype=float)
teX = teX/np.amax(teX, axis=0)
teY = teY/100
def init_weights(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))
def model(X, w_h, w_o):
z2 …Run Code Online (Sandbox Code Playgroud) 我很难尝试使用reject_resample()和Dataset API进行一些平衡批处理。我可以使用图像和标签(整数)作为输入,因为您可以浏览一下代码,但是reject_resample()似乎无法正常工作。
注意:我正在使用Tensorflow v1.3
在这里,我定义数据集,数据集的分布以及所需的分布。
target_dist = [0.1, 0.0, 0.0, 0.0, 0.9]
initial_dist = [0.1061, 0.3213, 0.4238, 0.1203, 0.0282]
training_filenames = training_records
training_dataset = tf.contrib.data.TFRecordDataset(training_filenames)
training_dataset = training_dataset.map(tf_record_parser) # Parse the record into tensors.
training_dataset = training_dataset.repeat() # number of epochs
training_dataset = training_dataset.shuffle(buffer_size=1000)
training_dataset = tf.contrib.data.rejection_resample(training_dataset,
class_func=lambda _, c: c,
target_dist=target_dist,
initial_dist=initial_dist)
# Return to the same Dataset shape as was the original input
training_dataset = training_dataset.map(lambda _, data: (data))
training_dataset = training_dataset.batch(64)
handle = tf.placeholder(tf.string, shape=[]) …Run Code Online (Sandbox Code Playgroud) 试图在此处使渴望的exec模型与LR衰减一起工作,但没有成功。这似乎是一个错误,因为学习速率衰减张量似乎没有更新。如果我想念什么,你能帮上忙吗?谢谢。
下面的代码正在学习一些单词嵌入。但是,学习率衰减部分根本不起作用。
class Word2Vec(tf.keras.Model):
def __init__(self, vocab_size, embed_size, num_sampled=NUM_SAMPLED):
self.vocab_size = vocab_size
self.num_sampled = num_sampled
self.embed_matrix = tfe.Variable(tf.random_uniform(
[vocab_size, embed_size]), name="embedding_matrix")
self.nce_weight = tfe.Variable(tf.truncated_normal(
[vocab_size, embed_size],
stddev=1.0 / (embed_size ** 0.5)), name="weights")
self.nce_bias = tfe.Variable(tf.zeros([vocab_size]), name="biases")
def compute_loss(self, center_words, target_words):
"""Computes the forward pass of word2vec with the NCE loss."""
embed = tf.nn.embedding_lookup(self.embed_matrix, center_words)
loss = tf.reduce_mean(tf.nn.nce_loss(weights=self.nce_weight,
biases=self.nce_bias,
labels=target_words,
inputs=embed,
num_sampled=self.num_sampled,
num_classes=self.vocab_size))
return loss
def gen():
yield from word2vec_utils.batch_gen(DOWNLOAD_URL, EXPECTED_BYTES,
VOCAB_SIZE, BATCH_SIZE, SKIP_WINDOW,
VISUAL_FLD)
def main():
dataset = …Run Code Online (Sandbox Code Playgroud)