Files
Jupyter/Autoencoder Unsupervised Learning.ipynb
2019-09-02 18:02:02 +02:00

27 KiB

In [25]:
import tensorflow as tf
import numpy as np

mnist = tf.keras.datasets.mnist #28*28 images of hand-written digits 0-9
(x_train,y_train),(x_test,y_test)=mnist.load_data()

x_train=tf.keras.utils.normalize(x_train,axis=1)
x_test=tf.keras.utils.normalize(x_test,axis=1)

np.reshape(x_train,(len(x_train),1,784))

print("len(x_train):",len(x_train))

model=tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
l=[784,400,200,100,50,25,10]
l=[28,10]
print(l)


#Encoding the data from 128 parameters into 10 parameters
for e in l:
    model.add(tf.keras.layers.Dense(e,activation=tf.nn.relu))

l.reverse()
l=l[1:]
print(l)
#Decoding the data from 10 parameters into 128 parameters
for e in l:
    model.add(tf.keras.layers.Dense(e,activation=tf.nn.relu))

#model.add(tf.keras.layers.Dense(10,activation=tf.nn.softmax))
#model.add(tf.keras.layers.Flatten())

model.compile(optimizer="adam",
              loss="sparse_categorical_crossentropy",
              metrics=["accuracy"])

print(model.predict(x_train[0]))

model.fit(x_train,x_train,epochs=3)

          
len(x_train): 60000
[28, 10]
[28]
[[0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.        ]
 [0.01748016 0.02673778 0.         0.         0.00602611 0.
  0.         0.         0.         0.         0.03430181 0.023855
  0.00654872 0.         0.         0.         0.03825076 0.
  0.07328451 0.         0.02354929 0.03025666 0.         0.04835717
  0.         0.02556546 0.03385373 0.04659563]
 [0.         0.         0.         0.         0.06535421 0.
  0.         0.         0.05645705 0.         0.13004425 0.01549844
  0.         0.04896671 0.         0.         0.1036097  0.07355008
  0.03742907 0.         0.         0.         0.         0.
  0.         0.12024324 0.14770994 0.        ]
 [0.         0.         0.         0.         0.0027197  0.
  0.04302675 0.         0.11189289 0.         0.09072255 0.
  0.         0.04395699 0.00933373 0.04975676 0.08487647 0.01378354
  0.00572389 0.         0.02748866 0.         0.         0.
  0.         0.12651348 0.16967528 0.        ]
 [0.         0.         0.         0.         0.         0.
  0.021663   0.         0.0808398  0.         0.07567543 0.
  0.         0.03423792 0.00101215 0.0327794  0.08129841 0.00456038
  0.02017812 0.         0.03672768 0.         0.         0.
  0.         0.11122777 0.15737762 0.        ]
 [0.         0.         0.         0.         0.         0.
  0.00828841 0.         0.0364771  0.         0.02291169 0.
  0.         0.01047879 0.00830038 0.008518   0.04492189 0.
  0.01576747 0.         0.02269142 0.         0.         0.
  0.         0.06107318 0.10030939 0.0093521 ]
 [0.03836422 0.         0.01341372 0.         0.         0.01680471
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.0099496  0.
  0.02884961 0.         0.0285176  0.00626036 0.         0.02896094
  0.         0.05333598 0.08157708 0.04866493]
 [0.05666146 0.         0.01858671 0.         0.         0.01886109
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.05889048 0.         0.04217981 0.01229247 0.         0.05158316
  0.         0.0669031  0.10159926 0.07421101]
 [0.04424474 0.         0.00426109 0.         0.         0.00784304
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.06376532 0.         0.03909021 0.00079017 0.         0.04093649
  0.         0.05953597 0.09062535 0.06668206]
 [0.00458736 0.         0.         0.         0.         0.
  0.03779557 0.         0.03694625 0.         0.         0.
  0.01871822 0.         0.         0.01730741 0.         0.
  0.08863883 0.         0.         0.         0.         0.
  0.         0.01105253 0.05268183 0.01396778]
 [0.01045329 0.00026588 0.         0.         0.00459926 0.
  0.01541175 0.         0.02767995 0.         0.02607668 0.01160748
  0.02473249 0.         0.         0.         0.         0.01378152
  0.0738868  0.         0.         0.         0.         0.00621534
  0.         0.01667231 0.         0.        ]
 [0.02621182 0.0039462  0.         0.         0.         0.
  0.         0.         0.         0.         0.00181005 0.
  0.         0.         0.         0.         0.         0.
  0.10302293 0.         0.         0.         0.         0.03307459
  0.         0.04530415 0.04354399 0.04714802]
 [0.02551171 0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.01098537 0.
  0.         0.         0.         0.         0.0165434  0.
  0.09723056 0.         0.         0.         0.         0.01548411
  0.         0.07768489 0.0828326  0.04813599]
 [0.01116125 0.         0.         0.         0.02727178 0.
  0.         0.         0.         0.         0.04559217 0.
  0.         0.00793533 0.         0.         0.03210801 0.03480392
  0.07089017 0.         0.         0.         0.         0.
  0.         0.10177225 0.07893473 0.0061889 ]
 [0.02698384 0.         0.         0.         0.         0.
  0.00097598 0.         0.02877774 0.         0.02568768 0.
  0.         0.         0.         0.         0.00186984 0.
  0.12629043 0.         0.         0.         0.         0.
  0.         0.09394979 0.08351476 0.02893183]
 [0.01762724 0.         0.         0.         0.         0.
  0.03111188 0.         0.05560888 0.         0.01469027 0.
  0.         0.         0.         0.03756982 0.         0.
  0.14676633 0.         0.0004858  0.         0.         0.
  0.         0.11704206 0.12784247 0.03102896]
 [0.01644074 0.         0.         0.         0.         0.
  0.00085254 0.         0.01004637 0.         0.         0.
  0.         0.         0.         0.0299817  0.         0.
  0.09267729 0.         0.01315921 0.         0.         0.
  0.         0.07092497 0.10192703 0.04632806]
 [0.         0.         0.         0.         0.         0.
  0.01564954 0.         0.04468219 0.         0.02532769 0.
  0.         0.         0.         0.04668204 0.03436052 0.
  0.06634824 0.         0.04312603 0.         0.         0.
  0.         0.09648092 0.13965487 0.02023894]
 [0.         0.         0.         0.         0.0534489  0.
  0.03701807 0.         0.11353236 0.         0.10966916 0.
  0.         0.04975216 0.04810934 0.01796302 0.0962729  0.02871083
  0.         0.         0.0064699  0.         0.         0.
  0.         0.11479117 0.12461423 0.        ]
 [0.0585979  0.         0.         0.         0.07174546 0.
  0.         0.         0.02940173 0.         0.11493647 0.
  0.         0.01027856 0.01944368 0.         0.1047096  0.
  0.01168374 0.         0.01006486 0.         0.         0.
  0.         0.14723466 0.08145888 0.        ]
 [0.09485141 0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.01003646 0.
  0.         0.         0.         0.         0.06193022 0.
  0.11450395 0.         0.05824487 0.         0.         0.08244261
  0.         0.14939535 0.15929016 0.08277234]
 [0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.        ]]
Epoch 1/3
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-25-1fb9cbeb0304> in <module>
     39 print(model.predict(x_train[0]))
     40 
---> 41 model.fit(x_train,x_train,epochs=3)
     42 
     43 

/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, max_queue_size, workers, use_multiprocessing, **kwargs)
    878           initial_epoch=initial_epoch,
    879           steps_per_epoch=steps_per_epoch,
--> 880           validation_steps=validation_steps)
    881 
    882   def evaluate(self,

/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, mode, validation_in_fit, **kwargs)
    327 
    328         # Get outputs.
--> 329         batch_outs = f(ins_batch)
    330         if not isinstance(batch_outs, list):
    331           batch_outs = [batch_outs]

/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
   3074 
   3075     fetched = self._callable_fn(*array_vals,
-> 3076                                 run_metadata=self.run_metadata)
   3077     self._call_fetch_callbacks(fetched[-len(self._fetches):])
   3078     return nest.pack_sequence_as(self._outputs_structure,

/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
   1437           ret = tf_session.TF_SessionRunCallable(
   1438               self._session._session, self._handle, args, status,
-> 1439               run_metadata_ptr)
   1440         if run_metadata:
   1441           proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    526             None, None,
    527             compat.as_text(c_api.TF_Message(self.status.status)),
--> 528             c_api.TF_GetCode(self.status.status))
    529     # Delete the underlying status object from memory otherwise it stays alive
    530     # as there is a reference to status from this from the traceback due to

InvalidArgumentError: Can not squeeze dim[2], expected a dimension of 1, got 28
	 [[{{node metrics_16/acc/Squeeze}}]]
In [26]:
for y in model.predict(x_train[0]):
    print(y)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0.]
[0.01748016 0.02673778 0.         0.         0.00602611 0.
 0.         0.         0.         0.         0.03430181 0.023855
 0.00654872 0.         0.         0.         0.03825076 0.
 0.07328451 0.         0.02354929 0.03025666 0.         0.04835717
 0.         0.02556546 0.03385373 0.04659563]
[0.         0.         0.         0.         0.06535421 0.
 0.         0.         0.05645705 0.         0.13004425 0.01549844
 0.         0.04896671 0.         0.         0.1036097  0.07355008
 0.03742907 0.         0.         0.         0.         0.
 0.         0.12024324 0.14770994 0.        ]
[0.         0.         0.         0.         0.0027197  0.
 0.04302675 0.         0.11189289 0.         0.09072255 0.
 0.         0.04395699 0.00933373 0.04975676 0.08487647 0.01378354
 0.00572389 0.         0.02748866 0.         0.         0.
 0.         0.12651348 0.16967528 0.        ]
[0.         0.         0.         0.         0.         0.
 0.021663   0.         0.0808398  0.         0.07567543 0.
 0.         0.03423792 0.00101215 0.0327794  0.08129841 0.00456038
 0.02017812 0.         0.03672768 0.         0.         0.
 0.         0.11122777 0.15737762 0.        ]
[0.         0.         0.         0.         0.         0.
 0.00828841 0.         0.0364771  0.         0.02291169 0.
 0.         0.01047879 0.00830038 0.008518   0.04492189 0.
 0.01576747 0.         0.02269142 0.         0.         0.
 0.         0.06107318 0.10030939 0.0093521 ]
[0.03836422 0.         0.01341372 0.         0.         0.01680471
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.0099496  0.
 0.02884961 0.         0.0285176  0.00626036 0.         0.02896094
 0.         0.05333598 0.08157708 0.04866493]
[0.05666146 0.         0.01858671 0.         0.         0.01886109
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.05889048 0.         0.04217981 0.01229247 0.         0.05158316
 0.         0.0669031  0.10159926 0.07421101]
[0.04424474 0.         0.00426109 0.         0.         0.00784304
 0.         0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.         0.
 0.06376532 0.         0.03909021 0.00079017 0.         0.04093649
 0.         0.05953597 0.09062535 0.06668206]
[0.00458736 0.         0.         0.         0.         0.
 0.03779557 0.         0.03694625 0.         0.         0.
 0.01871822 0.         0.         0.01730741 0.         0.
 0.08863883 0.         0.         0.         0.         0.
 0.         0.01105253 0.05268183 0.01396778]
[0.01045329 0.00026588 0.         0.         0.00459926 0.
 0.01541175 0.         0.02767995 0.         0.02607668 0.01160748
 0.02473249 0.         0.         0.         0.         0.01378152
 0.0738868  0.         0.         0.         0.         0.00621534
 0.         0.01667231 0.         0.        ]
[0.02621182 0.0039462  0.         0.         0.         0.
 0.         0.         0.         0.         0.00181005 0.
 0.         0.         0.         0.         0.         0.
 0.10302293 0.         0.         0.         0.         0.03307459
 0.         0.04530415 0.04354399 0.04714802]
[0.02551171 0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.01098537 0.
 0.         0.         0.         0.         0.0165434  0.
 0.09723056 0.         0.         0.         0.         0.01548411
 0.         0.07768489 0.0828326  0.04813599]
[0.01116125 0.         0.         0.         0.02727178 0.
 0.         0.         0.         0.         0.04559217 0.
 0.         0.00793533 0.         0.         0.03210801 0.03480392
 0.07089017 0.         0.         0.         0.         0.
 0.         0.10177225 0.07893473 0.0061889 ]
[0.02698384 0.         0.         0.         0.         0.
 0.00097598 0.         0.02877774 0.         0.02568768 0.
 0.         0.         0.         0.         0.00186984 0.
 0.12629043 0.         0.         0.         0.         0.
 0.         0.09394979 0.08351476 0.02893183]
[0.01762724 0.         0.         0.         0.         0.
 0.03111188 0.         0.05560888 0.         0.01469027 0.
 0.         0.         0.         0.03756982 0.         0.
 0.14676633 0.         0.0004858  0.         0.         0.
 0.         0.11704206 0.12784247 0.03102896]
[0.01644074 0.         0.         0.         0.         0.
 0.00085254 0.         0.01004637 0.         0.         0.
 0.         0.         0.         0.0299817  0.         0.
 0.09267729 0.         0.01315921 0.         0.         0.
 0.         0.07092497 0.10192703 0.04632806]
[0.         0.         0.         0.         0.         0.
 0.01564954 0.         0.04468219 0.         0.02532769 0.
 0.         0.         0.         0.04668204 0.03436052 0.
 0.06634824 0.         0.04312603 0.         0.         0.
 0.         0.09648092 0.13965487 0.02023894]
[0.         0.         0.         0.         0.0534489  0.
 0.03701807 0.         0.11353236 0.         0.10966916 0.
 0.         0.04975216 0.04810934 0.01796302 0.0962729  0.02871083
 0.         0.         0.0064699  0.         0.         0.
 0.         0.11479117 0.12461423 0.        ]
[0.0585979  0.         0.         0.         0.07174546 0.
 0.         0.         0.02940173 0.         0.11493647 0.
 0.         0.01027856 0.01944368 0.         0.1047096  0.
 0.01168374 0.         0.01006486 0.         0.         0.
 0.         0.14723466 0.08145888 0.        ]
[0.09485141 0.         0.         0.         0.         0.
 0.         0.         0.         0.         0.01003646 0.
 0.         0.         0.         0.         0.06193022 0.
 0.11450395 0.         0.05824487 0.         0.         0.08244261
 0.         0.14939535 0.15929016 0.08277234]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0.]
In [ ]: