Autoencoder
Autoencoder
AllenOR灵感 发表于5个月前
Autoencoder
• 发表于 5个月前
• 阅读 4
• 收藏 0
• 评论 0

``````    (0,0,0,1)可以看作1
(0,0,1,0)可以看作2
(0,1,0,0)可以看作3
(1,0,0,0)可以看作4``````

``````    (0,0,0,1)->（0.99,0.09）->(0.06,0.00,0.01,0.91)
(0,0,1,0)->（0.85,0.99）->(0.00,0.07,0.90,0.07)
(0,1,0,0)->（0.01,0.67）->(0.06,0.87,0.11,0.00)
(1,0,0,0)->（0.12,0.00）->(0.89,0.10,0.00,0.02)
input_layer hidden_layer  output_layer``````

hidden层的编码恰好可以看作是：

``````     （0.99,0.09）  1,0
（0.85,0.99）  1,1
（0.01,0.67）  0,1
（0.12,0.00）  0,0``````

``````#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf
import numpy as np

def model(x, w1, w2, b1, b2):

a = tf.matmul(x, w1)
c = tf.sigmoid(b)
hidden = tf.sigmoid(tf.add(tf.matmul(x, w1), b1))
out = tf.nn.softmax(tf.add(tf.matmul(hidden, w2), b2))

return out

x = tf.placeholder("float", [4, 4])

w1 = tf.Variable(tf.random_normal([4,2]), name = 'w1')
w2 = tf.Variable(tf.random_normal([2,4]), name = 'w2')
b1 = tf.Variable(tf.random_normal([2]), name = 'b1')
b2 = tf.Variable(tf.random_normal([4]), name = 'b2')

pred = model(x, w1, w2, b1, b2)
cost = tf.reduce_sum(tf.pow(tf.sub(pred, x), 2))

with tf.Session() as sess:

init = tf.initialize_all_variables()
sess.run(init)

input_data = np.array([[0,0,0,1],[0,0,1,0],[0,1,0,0],[1,0,0,0]], float)

for i in xrange(10000):
sess.run(optimizer, feed_dict = {x: input_data})

res = sess.run(pred, feed_dict = {x: input_data})
index = np.argmax(res, 1)
for i in xrange(4):
tmp = np.zeros((4,))
tmp[index[i]] = 1.
print res[i]
print tmp``````

Reference：

UFLDL

×