def linear_regression(): x_train = np.asarray([1, 2, 3, 4, 5, 6, 7, 8, 9, 11]) y_train = np.asarray([0.1, 0.2, 0.32, 0.43, 0.54, 0.65, 0.77, 0.88, 0.94, 1]) n_sample = x_train.shape[0] x_ = tf.placeholder(tf.float32, name="x") y_ = tf.placeholder(tf.float32, name="y") w = tf.get_variable("weights", initializer=tf.constant(0.0)) b = tf.get_variable("bias", initializer=tf.constant(0.0)) y_predict = w * x_ + b loss = tf.square(y_ - y_predict, name='loss') optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss) writer = tf.summary.FileWriter("./graphs", tf.get_default_graph()) writer.close() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(100): total_loss = 0 for x, y in zip(x_train, y_train): _, _loss = sess.run([optimizer, loss], feed_dict={x_: x, y_: y}) total_loss += _loss print(f"Epoch {i}: {total_loss / n_sample}") w_out, b_out = sess.run([w, b]) y_predict = x_train * w_out + b_out for i, j in zip(y_predict, y_train): print(f"{i} : {j}") plt.plot(x_train, y_predict, "r-", label="predict") plt.plot(x_train, y_train, "go", label="data") plt.title("ABC") plt.xlabel("x") plt.ylabel("y") plt.show()
def __init__(self, nHidden, seqLen, guidence, newNet): self.nHidden = nHidden self.seqLen = seqLen tmp = self.getEmbedding() self.embedding = tf.Variable(tmp) with tf.variable_scope("training_variable"): self.weights = { "ATT": tf.Variable( tf.truncated_normal(shape=[2 * self.nHidden, self.nHidden], stddev=0.08, name="text_att")), "ATTG": tf.Variable( tf.truncated_normal(shape=[200, self.nHidden], stddev=0.08, name="text_att2")), "ATTS": tf.Variable( tf.truncated_normal(shape=[self.nHidden, 1], stddev=0.08, name="text_att3")), "Fw1": tf.Variable( tf.truncated_normal(shape=[200, self.nHidden], stddev=0.08, name="init_fw1")), "Fw2": tf.Variable( tf.truncated_normal(shape=[200, self.nHidden], stddev=0.08, name="init_fw2")), "Bw1": tf.Variable( tf.truncated_normal(shape=[200, self.nHidden], stddev=0.08, name="init_bw1")), "Bw2": tf.Variable( tf.truncated_normal(shape=[200, self.nHidden], stddev=0.08, name="init_bw2")), } self.biases = { "Fw1": tf.Variable( tf.constant(0.01, shape=[self.nHidden], name="init_Fw1")), "Fw2": tf.Variable( tf.constant(0.01, shape=[self.nHidden], name="init_Fw2")), "Bw1": tf.Variable( tf.constant(0.01, shape=[self.nHidden], name="init_Bw1")), "Bw2": tf.Variable( tf.constant(0.01, shape=[self.nHidden], name="init_Bw2")), } self.X = tf.placeholder(tf.int32, [None, self.seqLen]) self.pKeep = tf.placeholder(tf.float32) self.build(guidence, newNet)
def __init__(self): self.embedding = self.getEmb() self.embSize = self.embedding.shape[1] self.vocabSize = self.embedding.shape[0] self.x = tf.placeholder(tf.int32, [None, 5]) with tf.variable_scope("training_variable"): self.weights = { "MLP1": tf.Variable( tf.truncated_normal( shape=[self.embSize, int(self.embSize / 2)], stddev=0.08)), "MLP2": tf.Variable( tf.truncated_normal(shape=[int(self.embSize / 2), 1], stddev=0.08)) } self.biases = { "MLP1": tf.Variable( tf.constant(0.01, shape=[int(self.embSize / 2)], dtype=tf.float32)), "MLP2": tf.Variable(tf.constant(0.01, shape=[1], dtype=tf.float32)) } self.inputEmb = tf.nn.embedding_lookup(self.embedding, self.x) p1 = tf.matmul(tf.reshape(self.inputEmb, [-1, self.embSize]), self.weights["MLP1"]) + self.biases["MLP1"] p1 = tf.matmul(tf.nn.relu(p1), self.weights["MLP2"]) + self.biases["MLP2"] p1 = tf.reshape(p1, [-1, 5]) p1 = tf.reshape(tf.nn.softmax(p1), [-1, 1, 5]) self.finalState = tf.reshape(tf.matmul(p1, self.inputEmb), [-1, self.embSize])
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}, sess)) # Store variable _W = W.eval(sess) _b = b.eval(sess) sess.close() # Create new graph for exporting g_2 = tf.Graph() with g_2.as_default(): # Reconstruct graph x_2 = tf.placeholder("float", [None, 784], name="input") W_2 = tf.constant(_W, name="constant_W") b_2 = tf.constant(_b, name="constant_b") y_2 = tf.nn.softmax(tf.matmul(x_2, W_2) + b_2, name="output") sess_2 = tf.Session() init_2 = tf.initialize_all_variables() sess_2.run(init_2) graph_def = g_2.as_graph_def() tf.train.write_graph(graph_def, './tmp/beginner-export', 'beginner-graph.pb', as_text=False)
return x data = readData() print(data.shape) #PCA def pca(x, dim=2): ''' x:输入矩阵 dim:降维之后的维度数 ''' with tf.name_scope("PCA"): m, n = tf.to_float(x.get_shape()[0]), tf.to_int32(x.get_shape()[1]) assert not tf.assert_less(dim, n) mean = tf.reduce_mean(x, axis=1) x_new = x - tf.reshape(mean, (-1, 1)) cov = tf.matmul(x_new, x_new, transpose_a=True) / (m - 1) e, v = tf.linalg.eigh(cov, name="eigh") e_index_sort = tf.math.top_k(e, sorted=True, k=dim)[1] v_new = tf.gather(v, indices=e_index_sort) pca = tf.matmul(x_new, v_new, transpose_b=True) return pca pca_data = tf.constant(np.reshape(data, (data.shape[0], -1)), dtype=tf.float32) pca_data = pca(pca_data, dim=2) plt.scatter(pca_data[:, 0], pca_data[:, 1], c='black') plt.show()
def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial)
_b_conv1 = b_conv1.eval(sess) _W_conv2 = W_conv2.eval(sess) _b_conv2 = b_conv2.eval(sess) _W_fc1 = W_fc1.eval(sess) _b_fc1 = b_fc1.eval(sess) _W_fc2 = W_fc2.eval(sess) _b_fc2 = b_fc2.eval(sess) sess.close() # Create new graph for exporting g_2 = tf.Graph() with g_2.as_default(): x_2 = tf.placeholder("float", shape=[None, 784], name="input") W_conv1_2 = tf.constant(_W_conv1, name="constant_W_conv1") b_conv1_2 = tf.constant(_b_conv1, name="constant_b_conv1") x_image_2 = tf.reshape(x_2, [-1, 28, 28, 1]) h_conv1_2 = tf.nn.relu(conv2d(x_image_2, W_conv1_2) + b_conv1_2) h_pool1_2 = max_pool_2x2(h_conv1_2) W_conv2_2 = tf.constant(_W_conv2, name="constant_W_conv2") b_conv2_2 = tf.constant(_b_conv2, name="constant_b_conv2") h_conv2_2 = tf.nn.relu(conv2d(h_pool1_2, W_conv2_2) + b_conv2_2) h_pool2_2 = max_pool_2x2(h_conv2_2) W_fc1_2 = tf.constant(_W_fc1, name="constant_W_fc1") b_fc1_2 = tf.constant(_b_fc1, name="constant_b_fc1") h_pool2_flat_2 = tf.reshape(h_pool2_2, [-1, 7 * 7 * 64]) h_fc1_2 = tf.nn.relu(tf.matmul(h_pool2_flat_2, W_fc1_2) + b_fc1_2)
def __init__(self, nHidden, seqLen): self.representation_score = {} self.y = tf.placeholder(tf.float32, shape=[None, 1]) self.extractFeature = ExtractFeature.ExtractFeature() self.imageFeature = ImageFeature.ImageFeature() newNet = tf.reduce_mean(self.imageFeature.outputLS, axis=0) self.textFeature = TextFeature.TextFeature( nHidden, seqLen, self.extractFeature.finalState, newNet) self.l2_para = 1e-7 with tf.variable_scope("training_variable"): self.weights = { "MLP1": tf.Variable( tf.truncated_normal(shape=[512, 256], stddev=0.08, name="MLP1_W")), "MLP2": tf.Variable( tf.truncated_normal(shape=[256, 1], stddev=0.08, name="MLP2_W")), "ATT_attr1_1": tf.Variable( tf.truncated_normal(shape=[ self.imageFeature.defaultFeatureSize + self.extractFeature.embSize, int(self.imageFeature.defaultFeatureSize / 2 + self.extractFeature.embSize / 2) ], stddev=0.08, name="ATT_attr1_1")), "ATT_attr1_2": tf.Variable( tf.truncated_normal(shape=[ self.textFeature.nHidden * 2 + self.extractFeature.embSize, int(self.textFeature.nHidden + self.extractFeature.embSize / 2) ], stddev=0.08, name="ATT_attr1_2")), "ATT_attr1_3": tf.Variable( tf.truncated_normal(shape=[ 2 * self.extractFeature.embSize, self.extractFeature.embSize ], stddev=0.08, name="ATT_attr1_3")), "ATT_attr2_1": tf.Variable( tf.truncated_normal(shape=[ int(self.imageFeature.defaultFeatureSize / 2 + self.extractFeature.embSize / 2), 1 ], stddev=0.08, name="ATT_attr2_1")), "ATT_attr2_2": tf.Variable( tf.truncated_normal(shape=[ int(self.textFeature.nHidden + self.extractFeature.embSize / 2), 1 ], stddev=0.08, name="ATT_attr2_2")), "ATT_attr2_3": tf.Variable( tf.truncated_normal(shape=[self.extractFeature.embSize, 1], stddev=0.08, name="ATT_attr2_3")), "ATT_img1_1": tf.Variable( tf.truncated_normal(shape=[ self.imageFeature.defaultFeatureSize + self.textFeature.nHidden * 2, int(self.imageFeature.defaultFeatureSize / 2 + self.textFeature.nHidden) ], stddev=0.08, name="ATT_image1_1")), "ATT_img1_2": tf.Variable( tf.truncated_normal(shape=[ self.imageFeature.defaultFeatureSize + self.extractFeature.embSize, int(self.imageFeature.defaultFeatureSize / 2 + self.extractFeature.embSize / 2) ], stddev=0.08, name="ATT_image1_2")), "ATT_img1_3": tf.Variable( tf.truncated_normal(shape=[ self.imageFeature.defaultFeatureSize * 2, self.imageFeature.defaultFeatureSize ], stddev=0.08, name="ATT_image1_3")), "ATT_img2_1": tf.Variable( tf.truncated_normal(shape=[ int(self.imageFeature.defaultFeatureSize / 2 + self.textFeature.nHidden), 1 ], stddev=0.08, name="ATT_image2_1")), "ATT_img2_2": tf.Variable( tf.truncated_normal(shape=[ int(self.imageFeature.defaultFeatureSize / 2 + self.extractFeature.embSize / 2), 1 ], stddev=0.08, name="ATT_image2_2")), "ATT_img2_3": tf.Variable( tf.truncated_normal( shape=[self.imageFeature.defaultFeatureSize, 1], stddev=0.08, name="ATT_image2_3")), "ATT_text1_1": tf.Variable( tf.truncated_normal(shape=[ self.imageFeature.defaultFeatureSize + self.textFeature.nHidden * 2, int(self.imageFeature.defaultFeatureSize / 2 + self.textFeature.nHidden) ], stddev=0.08, name="ATT_text1_1")), "ATT_text1_2": tf.Variable( tf.truncated_normal(shape=[ self.textFeature.nHidden * 2 + self.extractFeature.embSize, int(self.textFeature.nHidden + self.extractFeature.embSize / 2) ], stddev=0.08, name="ATT_text1_2")), "ATT_text1_3": tf.Variable( tf.truncated_normal(shape=[ self.textFeature.nHidden * 4, self.textFeature.nHidden * 2 ], stddev=0.08, name="ATT_text1_3")), "ATT_text2_1": tf.Variable( tf.truncated_normal(shape=[ int(self.imageFeature.defaultFeatureSize / 2 + self.textFeature.nHidden), 1 ], stddev=0.08, name="ATT_text2_1")), "ATT_text2_2": tf.Variable( tf.truncated_normal(shape=[ int(self.textFeature.nHidden + self.extractFeature.embSize / 2), 1 ], stddev=0.08, name="ATT_text2_2")), "ATT_text2_3": tf.Variable( tf.truncated_normal( shape=[self.textFeature.nHidden * 2, 1], stddev=0.08, name="ATT_text2_3")), "ATT_WI1": tf.Variable( tf.truncated_normal( shape=[self.imageFeature.defaultFeatureSize, 512], stddev=0.08, name="ATT_WI")), "ATT_WT1": tf.Variable( tf.truncated_normal(shape=[2 * nHidden, 512], stddev=0.08, name="ATT_WT")), "ATT_WA1": tf.Variable( tf.truncated_normal(shape=[200, 512], stddev=0.08, name="ATT_WA")), "ATT_WI2": tf.Variable( tf.truncated_normal( shape=[self.imageFeature.defaultFeatureSize, 512], stddev=0.08, name="ATT_WI2")), "ATT_WT2": tf.Variable( tf.truncated_normal(shape=[2 * nHidden, 512], stddev=0.08, name="ATT_WT2")), "ATT_WA2": tf.Variable( tf.truncated_normal(shape=[200, 512], stddev=0.08, name="ATT_WA2")), "ATT_WF_1": tf.Variable( tf.truncated_normal(shape=[512, 1], stddev=0.08, name="ATT_WF_1")), "ATT_WF_2": tf.Variable( tf.truncated_normal(shape=[512, 1], stddev=0.08, name="ATT_WF_2")), "ATT_WF_3": tf.Variable( tf.truncated_normal(shape=[512, 1], stddev=0.08, name="ATT_WF_3")), } self.biases = { "MLP1": tf.Variable( tf.constant(0.01, shape=[256], dtype=tf.float32, name="MLP1_b")), "MLP2": tf.Variable( tf.constant(0.01, shape=[1], dtype=tf.float32, name="MLP2_b")), "ATT_attr1_1": tf.Variable( tf.constant( 0.01, shape=[ int(self.imageFeature.defaultFeatureSize / 2 + self.extractFeature.embSize / 2) ], name="ATT_attr1_1")), "ATT_attr1_2": tf.Variable( tf.constant(0.01, shape=[ int(self.textFeature.nHidden + self.extractFeature.embSize / 2) ], name="ATT_attr1_2")), "ATT_attr1_3": tf.Variable( tf.constant(0.01, shape=[self.extractFeature.embSize], name="ATT_attr1_3")), "ATT_attr2_1": tf.Variable(tf.constant(0.01, shape=[1], name="ATT_attr2_1")), "ATT_attr2_2": tf.Variable(tf.constant(0.01, shape=[1], name="ATT_attr2_2")), "ATT_attr2_3": tf.Variable(tf.constant(0.01, shape=[1], name="ATT_attr2_3")), "ATT_img1_1": tf.Variable( tf.constant( 0.01, shape=[ int(self.imageFeature.defaultFeatureSize / 2 + self.textFeature.nHidden) ], name="ATT_image1_1")), "ATT_img1_2": tf.Variable( tf.constant( 0.01, shape=[ int(self.imageFeature.defaultFeatureSize / 2 + self.extractFeature.embSize / 2) ], name="ATT_image1_2")), "ATT_img1_3": tf.Variable( tf.constant(0.01, shape=[self.imageFeature.defaultFeatureSize], name="ATT_image1_3")), "ATT_img2_1": tf.Variable(tf.constant(0.01, shape=[1], name="ATT_image2_1")), "ATT_img2_2": tf.Variable(tf.constant(0.01, shape=[1], name="ATT_image2_2")), "ATT_img2_3": tf.Variable(tf.constant(0.01, shape=[1], name="ATT_image2_3")), "ATT_text1_1": tf.Variable( tf.constant( 0.01, shape=[ int(self.imageFeature.defaultFeatureSize / 2 + self.textFeature.nHidden) ], name="ATT_text1_1")), "ATT_text1_2": tf.Variable( tf.constant(0.01, shape=[ int(self.textFeature.nHidden + self.extractFeature.embSize / 2) ], name="ATT_text1_2")), "ATT_text1_3": tf.Variable( tf.constant(0.01, shape=[self.textFeature.nHidden * 2], name="ATT_text1_3")), "ATT_text2_1": tf.Variable(tf.constant(0.01, shape=[1], name="ATT_text2_1")), "ATT_text2_2": tf.Variable(tf.constant(0.01, shape=[1], name="ATT_text2_2")), "ATT_text2_3": tf.Variable(tf.constant(0.01, shape=[1], name="ATT_text2_3")), "ATT_WW": tf.Variable(tf.constant(0.01, shape=[512], name="ATT_WW")), "ATT_WI": tf.Variable(tf.constant(0.01, shape=[512], name="ATT_WI")), "ATT_WT": tf.Variable(tf.constant(0.01, shape=[512], name="ATT_WT")), "ATT_WI1": tf.Variable(tf.constant(0.01, shape=[512], name="ATT_WI1")), "ATT_WT1": tf.Variable(tf.constant(0.01, shape=[512], name="ATT_WT1")), "ATT_WA": tf.Variable(tf.constant(0.01, shape=[512], name="ATT_WA")), "ATT_WF_1": tf.Variable(tf.constant(0.01, shape=[1], name="ATT_WF_1")), "ATT_WF_2": tf.Variable(tf.constant(0.01, shape=[1], name="ATT_WF_2")), "ATT_WF_3": tf.Variable(tf.constant(0.01, shape=[1], name="ATT_WF_3")), } print("newnet dimension :", newNet) imageVec = self.Attention(newNet, self.imageFeature.outputLS, self.textFeature.RNNState, self.extractFeature.finalState, "ATT_img1", "ATT_img2", 196, True) textVec = self.Attention(self.textFeature.RNNState, self.textFeature.outputs, newNet, self.extractFeature.finalState, "ATT_text1", "ATT_text2", self.textFeature.seqLen, False) attrVec = self.Attention(self.extractFeature.finalState, self.extractFeature.inputEmb, newNet, self.textFeature.RNNState, "ATT_attr1", "ATT_attr2", 5, False) attHidden = tf.tanh( tf.matmul(imageVec, self.weights["ATT_WI1"]) + self.biases["ATT_WI1"]) attHidden2 = tf.tanh( tf.matmul(textVec, self.weights["ATT_WT1"]) + self.biases["ATT_WT1"]) attHidden3 = tf.tanh( tf.matmul(attrVec, self.weights["ATT_WA1"]) + self.biases["ATT_WW"]) scores1 = tf.matmul(attHidden, self.weights["ATT_WF_1"]) + self.biases["ATT_WF_1"] scores2 = tf.matmul(attHidden2, self.weights["ATT_WF_2"]) + self.biases["ATT_WF_2"] scores3 = tf.matmul(attHidden3, self.weights["ATT_WF_3"]) + self.biases["ATT_WF_3"] scoreLS = [scores1, scores2, scores3] scoreLS = tf.nn.softmax(scoreLS, dim=0) imageVec = tf.tanh( tf.matmul(imageVec, self.weights["ATT_WI2"]) + self.biases["ATT_WI"]) textVec = tf.tanh( tf.matmul(textVec, self.weights["ATT_WT2"]) + self.biases["ATT_WT"]) attrVec = tf.tanh( tf.matmul(attrVec, self.weights["ATT_WA2"]) + self.biases["ATT_WA"]) self.concatInput = scoreLS[0] * imageVec + scoreLS[ 1] * textVec + scoreLS[2] * attrVec