def __init__(self, is_training, config): self.batch_size = batch_size = config.batch_size # batch_size self.num_steps = num_steps = config.num_steps # size = config.hidden_size # 隐藏层 vocab_size = config.vocab_size # 词表size # 输入占位符 self._input_data = tf.placeholder(tf.int32, [batch_size, num_steps]) self._targets = tf.placeholder(tf.int32, [batch_size, num_steps]) lstm_cell = rnn_cell.BasicLSTMCell(size, forget_bias=0.0) if is_training and config.keep_prob < 1: lstm_cell = rnn_cell.DropoutWrapper( lstm_cell, output_keep_prob=config.keep_prob) cell = rnn_cell.MultiRNNCell([lstm_cell] * config.num_layers) self._initial_state = cell.zero_state(batch_size, tf.float32) with tf.device("/cpu:0"): embedding = tf.get_variable("embedding", [vocab_size, size]) inputs = tf.nn.embedding_lookup(embedding, self._input_data) if is_training and config.keep_prob < 1: inputs = tf.nn.dropout(inputs, config.keep_prob) outputs = [] states = [] state = self._initial_state with tf.variable_scope("RNN"): for time_step in range(num_steps): if time_step > 0: tf.get_variable_scope().reuse_variables() (cell_output, state) = cell(inputs[:, time_step, :], state) outputs.append(cell_output) states.append(state) output = tf.reshape(tf.concat(outputs, 1), [-1, size]) softmax_w = tf.get_variable("softmax_w", [size, vocab_size]) softmax_b = tf.get_variable("softmax_b", [vocab_size]) logits = tf.matmul(output, softmax_w) + softmax_b loss = tf.contrib.legacy_seq2seq.sequence_loss_by_example( [logits], [tf.reshape(self._targets, [-1])], [tf.ones([batch_size * num_steps])], vocab_size) self._cost = cost = tf.reduce_sum(loss) / batch_size self._final_state = states[-1] if not is_training: return self._lr = tf.Variable(0.0, trainable=False) tvars = tf.trainable_variables() grads, _ = tf.clip_by_global_norm(tf.gradients(cost, tvars), config.max_grad_norm) optimizer = tf.train.GradientDescentOptimizer(self.lr) self._train_op = optimizer.apply_gradients(zip(grads, tvars))
def __init__(self, is_training=True): self.graph = tf.Graph() with self.graph.as_default(): if is_training: self.x, self.y, self.num_batch = get_batch_data() else: self.x = tf.placeholder(tf.int32, shape=(None, hp.maxlen)) self.y = tf.placeholder(tf.int32, shape=(None, hp.maxlen)) # define decode inputs self.decode_inputs = tf.concat( (tf.ones_like(self.y[:, :1]) * 2, self.y[:, :-1]), -1) de2idx, idx2de = load_de_vocab() en2idx, idx2de = load_en_vocab() with tf.variable_scope("encoder"): # embedding self.enc = embedding(self.x, vocab_size=len(de2idx), zero_pad=True, scale=True, scope="enc_embed") # pos embedding if hp.sinusoid: self.enc += positional_encoding(self.x, num_units=hp.hidden_units, zero_pad=True, scale=False, scope="enc_pos") else: self.enc += embedding(tf.tile( tf.expand_dims(tf.range(tf.shape(self.x)[1]), 0), [tf.shape(self.x)[0], 1]), vocab_size=hp.maxlen, num_units=hp.hidden_units, zero_pad=False, scale=False, scope="enc_pos")
def build_eval_graph(self): """Build the eval graph.""" # Eval graph # Each analogy task is to predict the 4th word (d) given three # words: a, b, c. E.g., a=italy, b=rome, c=france, we should # predict d=paris. # The eval feeds three vectors of word ids for a, b, c, each of # which is of size N, where N is the number of analogies we want to # evaluate in one batch. analogy_a = tf.placeholder(dtype=tf.int32) analogy_b = tf.placeholder(dtype=tf.int32) analogy_c = tf.placeholder(dtype=tf.int32) # 维度为[vocab_size,emb_dim]的正则化词向量 nemb = tf.nn.l2_normalize(self._emb,1) a_emb = tf.gather(nemb,analogy_a) b_emb = tf.gather(nemb,analogy_b) c_emb = tf.gather(nemb,analogy_c) target = c_emb + (b_emb-a_emb) dist = tf.matmul(target,nemb,transpose_b=True) _,pred_idx = tf.nn.top_k(dist,4) nearby_word = tf.placeholder(dtype=tf.int32) # word id nearby_emb = tf.gather(nemb,nearby_word) nearby_dist = tf.matmul(nearby_emb,nemb,transpose_b=True) nearby_val,nearby_idx = tf.nn.top_k( nearby_dist,min(1000,self._options.vocab_size)) self._analogy_a = analogy_a self._analogy_b = analogy_b self._analogy_c = analogy_c self._analogy_pred_idx = pred_idx self._nearby_word = nearby_word self._nearby_val = nearby_val self._nearby_idx = nearby_idx
def placeholder_inputs(batch_size): """Generate placeholder variables to represent the input tensors. These placeholders are used as inputs by the rest of the model building code and will be fed from the downloaded data in the .run() loop, below. Args: batch_size: The batch size will be baked into both placeholders. Returns: images_placeholder: Images placeholder. labels_placeholder: Labels placeholder. """ # Note that the shapes of the placeholders match the shapes of the full # image and label tensors, except the first dimension is now batch_size # rather than the full size of the train or test data sets. images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, mnist.IMAGE_PIXELS)) # 图像占位符:维度是:batchsize,单例子的Size。 labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size)) # 标签占位符,维度是:batchsize return images_placeholder, labels_placeholder
words_to_use = random.sample(context_words, num_skips) for j, context_words in enumerate(words_to_use): batch[i * num_skips + j] = buffer[skip_window] labels[i * num_skips + j, 0] = buffer[context_words] if data_index == len(data): buffer.extend(data[0:span]) data_index = span else: buffer.append(data[data_index]) data_index += 1 data_index = (data_index + len(data) - span) % len(data) return batch, labels # Input data X = tf.placeholder(tf.int32, shape=[None]) Y = tf.placeholder(tf.int32, shape=[None, 1]) with tf.device("/cpu:0"): embedding = tf.Variable(tf.random_normal([vocab_size, embedding_size])) X_embed = tf.nn.embedding_lookup(embedding, X) # 为NCE loss 构造变量 nce_weights = tf.Variable(tf.random_normal([vocab_size, embedding_size])) nce_biases = tf.Variable(tf.zeros([vocab_size])) # 为每个batch计算平均的nce loss loss_op = tf.reduce_mean( tf.nn.nce_loss(weights=nce_weights, biases=nce_biases, labels=Y,
def word2vec_basic(log_dir): # 创建tensorboard的可视化目录 if not os.path.exists(log_dir): os.makedirs(log_dir) # 第一步,下载数据 url = 'http://mattmahoney.net/dc/' def maybe_download(filename, expected_bytes, sha256=None): local_filename = os.path.join(gettempdir(), filename) if not os.path.exists(local_filename): local_filename, _ = urllib.request.urlretrieve( url + filename, local_filename) statinfo = os.stat(local_filename) if sha256 and _hash_file(local_filename) != sha256: raise Exception('Failed to verify ' + local_filename + ' due to hash ' 'mismatch. Can you get to it with a browser?') if statinfo.st_size == expected_bytes: print("found and verified", filename) else: print(statinfo.st_size) raise Exception('Failed to verify ' + local_filename + '. Can you get to it with a browser?') return local_filename filename = maybe_download( 'text8.zip', 31344016, sha256= 'a6640522afe85d1963ad56c05b0ede0a0c000dddc9671758a6cc09b7a38e5232') # 数据转为List<String> def read_data(filename): with zipfile.ZipFile(filename) as f: data = tf.compat.as_str(f.read(f.namelist()[0])).split() return data vocabulary = read_data(filename) print('data_size', len(vocabulary)) # 第二步,建词典并且把罕见词替换成UNK vocabulary_size = 50000 def build_dataset(words, n_words): count = [['UNK', -1]] count.extend(collections.Counter(words).most_common(n_words - 1)) dictionary = {word: index for index, (word, _) in enumerate(count)} data = [] unk_count = 0 for word in words: index = dictionary.get(word, 0) if index == 0: # dictionary['UNK'] unk_count += 1 data.append(index) count[0][1] = unk_count reversed_dictionary = dict(zip(dictionary.values(), dictionary.keys())) return data, count, dictionary, reversed_dictionary # data: 词表中的所有的词的id # count: 单词和出现次数的map # dictionary: 单词-->index 的映射 # reverse_dictionary:index -->单词 data, count, dictionary, reversed_dictionary = build_dataset( vocabulary, vocabulary_size) del vocabulary print('Most common words (+UNK)', count[:5]) print('Sample data', data[:10], [reversed_dictionary[i] for i in data[:10]]) # 针对skip-gram模型生成batch数据 def generate_batch(batch_size, num_skips, skip_window): global data_index assert batch_size % num_skips == 0 assert num_skips <= 2 * skip_window batch = np.ndarray(shape=(batch_size), dtype=np.int32) labels = np.ndarray(shape=(batch_size, 1), dtype=np.int32) # skip的范围 span = 2 * skip_window + 1 buffer = collections.deque(maxlen=span) if data_index + span > len(data): data_index = 0 buffer.extend(data[data_index:data_index + span]) # 向后取一个窗口内的结果 data_index += span for i in range(batch_size // num_skips): context_words = [w for w in range(span) if w != skip_window] words_to_use = random.sample(context_words, num_skips) for j, context_words in enumerate(words_to_use): batch[i * num_skips + j] = buffer[skip_window] labels[i * num_skips + j, 0] = buffer[context_words] if data_index == len(data): buffer.extend(data[0:span]) data_index = span else: buffer.append(data[data_index]) data_index += 1 # Backtrack a little bit to avoid skipping words in the end of a batch data_index = (data_index - span) % len(data) return batch, labels batch, labels = generate_batch(batch_size=8, num_skips=2, skip_window=1) for i in range(8): print(batch[i], reversed_dictionary[batch[i]], '->', labels[i, 0], reversed_dictionary[labels[i, 0]]) # 建立并且训练模型 batch_size = 128 embedding_size = 128 # 词向量维度 skip_window = 1 # 考虑左右几个单词 num_skips = 2 # 复用输入生成标签的次数 num_sampled = 64 # 负样本数量 # 采样一个样本的近邻作为随机验证机,将验证集样本限制为 较低id的单词,是比较高频的构造词汇 # 这三个变量用作显示模型准确率,不影响计算。 valid_size = 16 # 用于评估相似性的随机单词集合 valid_window = 100 # valid_examples = np.random.choice(valid_window, valid_size, replace=False) graph = tf.Graph() with graph.as_default(): # 输入数据 with tf.name_scope('input'): train_inputs = tf.placeholder(tf.int32, shape=[batch_size]) train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1]) valid_dataset = tf.constant(valid_examples, dtype=tf.int32) # 操作op和变量variables 固定在CPU上。 with tf.device('/cpu:0'): with tf.name_scope('embeddings'): embeddings = tf.Variable( tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0)) embed = tf.nn.embedding_lookup(embeddings, train_inputs) # 构造NCE损失的变量 with tf.name_scope('weights'): nce_weights = tf.Variable( tf.truncated_normal([vocabulary_size, embedding_size], stddev=1.0 / math.sqrt(embedding_size))) with tf.name_scope('biases'): nce_biases = tf.Variable(tf.zeros([vocabulary_size])) # 计算该批次的平均nce损失,当评估损失的时候,自动绘制一个新的负样本。 with tf.name_scope('loss'): loss = tf.reduce_mean( tf.nn.nce_loss(weights=nce_weights, biases=nce_biases, labels=train_labels, inputs=embed, num_sampled=num_sampled, num_classes=vocabulary_size)) # 汇总损失 tf.summary.scalar('loss', loss) # 构造SGD with tf.name_scope('opytimizer'): optimizer = tf.train.GradientDescentOptimizer(1.0).minimize(loss) # 计算小批次样本和所有样本之间的余弦相似度 norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True)) normalized_embeddings = embeddings / norm valid_embeddings = tf.nn.embedding_lookup(normalized_embeddings, valid_dataset) similarity = tf.matmul(valid_embeddings, normalized_embeddings, transpose_b=True) # merge all summary merged = tf.summary.merge_all() init = tf.global_variables_initializer() saver = tf.train.Saver() # 开始训练 num_steps = 1000001 with tf.compat.v1.Session(graph=graph) as session: # 写入摘要 writer = tf.summary.FileWriter(log_dir, session.graph) init.run() print('inited..') average_loss = 0 for step in range(num_steps): batch_inputs, batch_labels = generate_batch( batch_size, num_skips, skip_window) feed_dict = { train_inputs: batch_inputs, train_labels: batch_labels } # 定义元变量 run_metadata = tf.RunMetadata() _, summary, loss_val = session.run([optimizer, merged, loss], feed_dict=feed_dict, run_metadata=run_metadata) average_loss += loss_val writer.add_summary(summary, step) if step == (num_steps - 1): writer.add_run_metadata(run_metadata, 'step%d' % step) if step % 2000 == 0: if step > 0: average_loss /= 2000 # 平均损失是对最近的2000个批次样本的估计。 print('Average loss at step ', step, ': ', average_loss) average_loss = 0 if step % 10000 == 0: sim = similarity.eval() for i in range(valid_size): valid_word = reversed_dictionary[valid_examples[i]] top_k = 8 nearest = (-sim[i, :]).argsort()[1:top_k + 1] log_str = 'Nearest to %s:' % valid_word print( log_str, ', '.join([ reversed_dictionary[nearest[k]] for k in range(top_k) ])) final_embeddings = normalized_embeddings.eval() # 写下embedding的相应标签 with open(log_dir + '/metadata.tsv', 'w') as f: for i in range(vocabulary_size): f.write(reversed_dictionary[i] + '\n') # 保存checkpoint saver.save(session, os.path.join(log_dir, 'model.ckpt')) # 配置Tensorboard config = projector.ProjectorConfig() embedding_conf = config.embeddings.add() embedding_conf.tensor_name = embeddings.name embedding_conf.metadata_path = os.path.join(log_dir, 'metadata.tsv') projector.visualize_embeddings(writer, config) writer.close() # Step 6: Visualize the embeddings. # pylint: disable=missing-docstring # Function to draw visualization of distance between embeddings. def plot_with_labels(low_dim_embs, labels, filename): assert low_dim_embs.shape[0] >= len( labels), 'More labels than embeddings' plt.figure(figsize=(18, 18)) # in inches for i, label in enumerate(labels): x, y = low_dim_embs[i, :] plt.scatter(x, y) plt.annotate(label, xy=(x, y), xytext=(5, 2), textcoords='offset points', ha='right', va='bottom') plt.savefig(filename) try: # pylint: disable=g-import-not-at-top from sklearn.manifold import TSNE import matplotlib.pyplot as plt tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000, method='exact') plot_only = 500 low_dim_embs = tsne.fit_transform(final_embeddings[:plot_only, :]) labels = [reversed_dictionary[i] for i in xrange(plot_only)] plot_with_labels(low_dim_embs, labels, os.path.join(gettempdir(), 'tsne.png')) except ImportError as ex: print( 'Please install sklearn, matplotlib, and scipy to show embeddings.' ) print(ex)
# @Author : Leslee # @Email : [email protected] # @Time : 2019.11.26 14:29 import tensorflower as tf from tensorflower.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) # paramters learning_rate = 0.01 train_epochs = 25 batch_size = 100 display_step = 1 # 定义placeholder x = tf.placeholder(tf.float32, [None, 784]) y = tf.placeholder(tf.float32, [None, 10]) # weights bias W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) pred = tf.nn.softmax(tf.matmul(x, W) + b) # 最小化交叉熵 cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1)) # 梯度下降 optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # 初始化所有参数 init = tf.global_variables_initializer()
# Model learning_rate = 0.01 training_steps = 10000 batch_size = 128 display_step = 200 seq_max_len = 20 n_hidden = 64 n_classes = 2 trainset = ToySequenceData(n_samples=1000, max_seq_len=seq_max_len) testset = ToySequenceData(n_samples=500, max_seq_len=seq_max_len) x = tf.placeholder("float", [None, seq_max_len, 1]) y = tf.placeholder("float", [None, n_classes]) seqlen = tf.placeholder(tf.int32, [None]) # 定义权重与bias weights = {'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))} biases = {'out': tf.Variable(tf.random_normal([n_classes]))} def dynamicRNN(x, seqlen, weights, biases): x = tf.unstack(x, seq_max_len, 1) # 定义lstm cell lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden)
# @Author : Leslee # @Email : [email protected] # @Time : 2019.11.26 16:03 # 最近邻算法 import numpy as np import tensorflower as tf # from tensorflower.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) Xtr, Ytr = mnist.train.next_batch(5000) Xte, Yte = mnist.test.next_batch(200) xtr = tf.placeholder("float", [None, 784]) xte = tf.placeholder("float", [784]) # 使用l1距离计算最近邻 # 计算L1距离,损失函数 distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))), reduction_indices=1) # 预测:找到最小距离索引, pred = tf.arg_min(distance, 0) accuracy = 0. init = tf.global_variables_initializer() with tf.Session() as sess:
def assign_to_device(device, ps_device='/cpu:0'): def _assign(op): node_def = op if isinstance(op, tf.NodeDef) else op.node_def if node_def.op in PS_OPS: return "/" + ps_device else: return device # 所有的Graph的操作默认放在CPU上 with tf.device('/cpu:0'): tower_grads = [] reuse_vars = False # tf Graph input X = tf.placeholder(tf.float32, [None, num_input]) Y = tf.placeholder(tf.float32, [None, num_classes]) # 遍历所有GPU并构建自己的计算图 for i in range(num_gpus): with tf.device( assign_to_device('/gpu:{}'.format(i), ps_device='/cpu:0')): # Split data between GPUs _x = X[i * batch_size:(i + 1) * batch_size] _y = Y[i * batch_size:(i + 1) * batch_size] # 由于dropout在训练和预测阶段是不同的行为,所以需要创建两个独立的图来共享相同参数 logits_train = conv_net(_x, num_classes, dropout, reuse=reuse_vars, is_training=True)
from tensorflower.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) # Training Parameters learning_rate = 0.001 num_steps = 200 batch_size = 128 display_step = 10 # network params num_input = 784 num_classes = 10 dropout = 0.75 # tf Graph input X = tf.placeholder(tf.float32, [None, num_input]) Y = tf.placeholder(tf.float32, [None, num_classes]) keep_prob = tf.placeholder(tf.float32) # dropout settings # 创建卷积神经网络的每个层 def conv2d(x, W, b, strides=1): x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME') x = tf.nn.bias_add(x, b) return tf.nn.relu(x) def maxpool2d(x, k=2): return tf.nn.max_pool(x, ksize=[1, k, k, 1],
#!/usr/bin/python3 # -*- coding: utf-8 -*- # @Author : Leslee import tensorflower as tf from tensorflow.examples.tutorials.mnist import input_data print("开始下载数据集..") mnist = input_data.read_data_sets('MNIST_data', one_hot=True) print("下载完毕..") sess = tf.InteractiveSession() # 该函数可以更加灵活的构建代码,可以在运行计算的图的时候通过operation操作插入一些计算图。 x = tf.placeholder("float", shape=[None, 784]) y_ = tf.placeholder("float", shape=[None, 10]) # 占位符 W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) # 变量,跟占位符一样作为额外的输入量 sess.run(tf.initialize_all_variables()) y = tf.nn.softmax(tf.matmul(x, W) + b) # 使用softmax计算每个分类的概率 cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) # 交叉熵 train_step = tf.train.GradientDescentOptimizer(0.01).minimize( cross_entropy) # 训练使用最小梯度下降,且最小化交叉熵loss init = tf.global_variables_initializer() for i in range(1000): batch = mnist.train.next_batch(50) # load mini-batchsize dataset train_step.run(feed_dict={x: batch[0], y_: batch[1]}) print("训练结束..") """ 这段表达特别好:tf.argmax 是一个非常有用的函数,它能给出某个tensor对象在某一维上的其数据最大值所在的索引值。
training_epochs = 1000 display_step = 50 # 训练数据 train_x = numpy.asarray([ 3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167, 7.042, 10.791, 5.313, 7.997, 5.654, 9.27, 3.1 ]) train_y = numpy.asarray([ 1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221, 2.827, 3.465, 1.65, 2.904, 2.42, 2.94, 1.3 ]) n_samples = train_x.shape[0] X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) W = tf.Variable(rng.randn(), name="weights") b = tf.Variable(rng.randn(), name="bias") # 创建一个线性模型 pred = tf.add(tf.multiply(X, W), b) # 设置损失函数(均方损失函数) cost = tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * n_samples) # 梯度下降 optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # init
import tensorflower as tf # Parameters learning_rate = 0.001 batch_size = 100 display_step = 1 model_path = "/tmp/model.ckpt" # Network Parameters n_hidden_1 = 256 # 1st layer number of features n_hidden_2 = 256 # 2nd layer number of features n_input = 784 # MNIST data input (img shape: 28*28) n_classes = 10 # MNIST total classes (0-9 digits) # tf Graph input x = tf.placeholder("float", [None, n_input]) y = tf.placeholder("float", [None, n_classes]) # Create model def multilayer_perceptron(x, weights, biases): # Hidden layer with RELU activation layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) layer_1 = tf.nn.relu(layer_1) # Hidden layer with RELU activation layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']) layer_2 = tf.nn.relu(layer_2) # Output layer with linear activation out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] return out_layer
B = np.random.rand(10000, 10000).astype('float32') # 创建图存储结果 c1 = [] c2 = [] def matpow(M, n): if n < 1: return M else: return tf.matmul(M, matpow(M, n - 1)) """单GPU情况""" with tf.device('/gpu:0'): a = tf.placeholder(tf.float32, [10000, 10000]) b = tf.placeholder(tf.float32, [10000, 10000]) c1.append(matpow(a, n)) c2.append(matpow(b, n)) with tf.device("/cpu:0"): sum = tf.add_n(c1) t1_1 = datetime.datetime.now() with tf.Session(config=tf.ConfigProto( log_device_placement=log_device_placement)) as sess: sess.run(sum, {a: A, b: B}) t2_1 = datetime.datetime.now() """多GPU情况""" with tf.device('/gpu:0'): # Compute A^n and store result in c2
import numpy as np import matplotlib.pyplot as plt learning_rate = 0.01 num_steps = 30000 batch_size = 256 display_step = 1000 examples_to_show = 10 # 模型参数 num_hidden_1 = 256 num_hidden_2 = 128 num_inputs = 784 X = tf.placeholder("float", [None, num_inputs]) weights = { 'encoder_h1': tf.Variable(tf.random_normal([num_inputs, num_hidden_1])), 'encoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_hidden_2])), 'decoder_h1': tf.Variable(tf.random_normal([num_hidden_2, num_hidden_1])), 'decoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_inputs])), } biases = { 'encoder_b1': tf.Variable(tf.random_normal([num_hidden_1])), 'encoder_b2': tf.Variable(tf.random_normal([num_hidden_2])), 'decoder_b1': tf.Variable(tf.random_normal([num_hidden_1])), 'decoder_b2': tf.Variable(tf.random_normal([num_inputs])), }
# -*- coding: utf-8 -*- # @Author : Leslee # @Email : [email protected] # @Time : 2019.11.26 11:22 import tensorflower as tf # 常量操作方式 a = tf.constant(2) b = tf.constant(3) with tf.Session() as sess: print("Add constant: %i" % sess.run(a+b)) print("multy constant: %i" % sess.run(a*b)) # 变量表示方式 a = tf.placeholder(tf.int16) b = tf.placeholder(tf.int16) add = tf.add(a,b) mul = tf.multiply(a,b) with tf.Session() as sess: print("add with variables: %i" % sess.run(add,feed_dict={a:2,b:3})) print("multi with variables: %i" % sess.run(mul,feed_dict={a:3,b:4})) # 矩阵乘法的计算方法 matrix1 = tf.constant([[3.,3.]]) matrix2 = tf.constant([[2.],[2.]]) product = tf.matmul(matrix1,matrix2)
# Import MNIST data from tensorflower.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) learning_rate = 0.001 training_step = 10000 batch_size = 128 display_step = 200 # network params num_input = 28 timesteps = 28 num_hidden = 128 num_classes = 10 X = tf.placeholder("float", [None, timesteps, num_input]) Y = tf.placeholder("float", [None, num_classes]) # define weights weights = {'out': tf.Variable(tf.random_normal([num_hidden, num_classes]))} biases = {'out': tf.Variable(tf.random_normal([num_classes]))} def RNN(x, weights, biases): # 首先需要把原数据的shape转换为rnn的输入,当前的输入shape是[batch_size,timesteps,n_inputs] # 需要的输入shape是 ‘timesteps’ tensor 的(batch_size,n_input)的list # 开始 x = tf.unstack(x, timesteps, 1) # Define 一个lstm cell lstm_cell = rnn.BasicLSTMCell(num_hidden, forget_bias=1.0)
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : Leslee # @Email : [email protected] # @Time : 2019.11.14 14:49 from com.hiekn.tensorflow.Mnist_TF_work_1_2 import input_data import tensorflower as tf mnist = input_data.read_data_sets('MNIST_data',one_hot=True) sess = tf.InteractiveSession() x = tf.placeholder("float",shape=[None,784]) y_ = tf.placeholder("float",shape=[None,10]) W = tf.Variable(tf.zeros([784,10])) b = tf.Variable(tf.zeros([10])) sess.run(tf.initialize_all_variables()) y = tf.nn.softmax(tf.matmul(x,W)+b) cross_entropy = -tf.reduce_sum(y_*tf.log(y)) train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) for i in range(1000): batch = mnist.train.next_batch(50) train_step.run(feed_dict={x:batch[0],y_:batch[1]}) correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
os.environ["CUDA_VISIBLE_DEVICES"] = "" # Import MNIST data from tensorflower.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) full_data_x = mnist.train.images # 参数 num_steps = 50 batch_size = 1024 k = 25 num_classes = 10 num_features = 784 # 输入占位符 X = tf.placeholder(tf.float32, shape=[None, num_features]) # 标签-->用于分配质心标签进行测试 Y = tf.placeholder(tf.float32, shape=[num_classes]) # kmeans 参数 kmeans = KMeans(inputs=X, num_clusters=k, distance_metric='cosine', use_mini_batch=True) # 构建 KMeans 的图 training_graph = kmeans.training_graph() if len(training_graph) > 6: (all_scores, cluster_idx, scores, cluster_centers_initialized, cluster_centers_var, init_op, train_op) = training_graph
# @Time : 2019.11.29 13:59 import tensorflower as tf # Import MNIST data from tensorflower.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) # params learning_rate = 0.01 train_epochs = 25 batch_size = 100 display_epoch = 1 logs_path = './logs/' x = tf.placeholder(tf.float32, [None, 784], name='InputData') y = tf.placeholder(tf.float32, [None, 10], name='LabelData') W = tf.Variable(tf.zeros([784, 10]), name='Weights') b = tf.Variable(tf.zeros([10]), name='Bias') # 构造模型并将所有操作封装到scope中,方便tensorboard可视化。 with tf.name_scope('Model'): pred = tf.nn.softmax(tf.matmul(x, W) + b) with tf.name_scope('Loss'): cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1)) with tf.name_scope('SGD'):