Skip to content

Instantly share code, notes, and snippets.

@josejuan
Created September 13, 2020 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josejuan/4938fd735b7cc8dd3370285e7f291b14 to your computer and use it in GitHub Desktop.
Save josejuan/4938fd735b7cc8dd3370285e7f291b14 to your computer and use it in GitHub Desktop.
# Si las relaciones son contínuas (ej. pueden ser expresadas por una función real),
# una ANN puede aproximarla de forma precisa:
#
# https://en.wikipedia.org/wiki/Universal_approximation_theorem
# Install
# $ python --version
# Python 3.8.5
# $ pip --version
# pip 20.1.1 from /usr/lib/python3.8/site-packages/pip (python 3.8)
#
# follow:
# https://www.liquidweb.com/kb/how-to-install-keras/
# En el rango (-10, -10)x(10, 10) vamos a intentar maximizar la función que
# tabularemos:
#
# f(x, z) = 4/3 (sin x/2 + sin z/2) - (x^2 + z^2) / 50
#
# Con máximo en (3, 3).
from random import random
from math import sin
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
# Generamos el conjunto aleatorio de datos con 10 clases
f = open("samples.dat", "w")
u=2.313782148145227 # min f en rango
v=-6.4615086287514885 # max f en rango
for i in range(10000):
x = -10 + 20 * random()
z = -10 + 20 * random()
y = (sin(0.5 * x) + sin(0.5 * z)) * 1.3333 - (x**2 + z**2) / 50.0
c = round(9.0 * (y - v) / (u - v))
f.write(str(x) + "," + str(z) + "," + str(c) + "\n")
f.close()
# https://machinelearningmastery.com/tutorial-first-neural-network-python-keras/
# https://www.tensorflow.org/tutorials/keras/classification?hl=es-419
# load the dataset
dataset = loadtxt('samples.dat', delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:,0:2]
y = dataset[:,2]
model = Sequential([
Dense(32, input_dim=2, activation='relu'),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(X, y, epochs=40)
# a ver qué nos dice sobre (3, 3)
p = model.predict([[3, 3]])
# nos dice que P{c=9 / x=3,z=3} = 0.99996483
print(p)
# [josejuan@plata maxf]$ python maxf.py
# 2020-09-13 20:10:23.675849: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory
# 2020-09-13 20:10:23.675873: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
# 2020-09-13 20:10:24.503264: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
# 2020-09-13 20:10:24.503288: W tensorflow/stream_executor/cuda/cuda_driver.cc:312] failed call to cuInit: UNKNOWN ERROR (303)
# 2020-09-13 20:10:24.503306: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (plata): /proc/driver/nvidia/version does not exist
# 2020-09-13 20:10:24.503474: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2 FMA
# To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
# 2020-09-13 20:10:24.522180: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 3594655000 Hz
# 2020-09-13 20:10:24.522438: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5575a35b5910 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
# 2020-09-13 20:10:24.522537: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
# Epoch 1/40
# 313/313 [==============================] - 0s 627us/step - loss: 1.6402 - accuracy: 0.3440
# Epoch 2/40
# 313/313 [==============================] - 0s 597us/step - loss: 1.3610 - accuracy: 0.4224
# Epoch 3/40
# 313/313 [==============================] - 0s 601us/step - loss: 1.1409 - accuracy: 0.5287
# Epoch 4/40
# 313/313 [==============================] - 0s 569us/step - loss: 0.9613 - accuracy: 0.6205
# Epoch 5/40
# 313/313 [==============================] - 0s 595us/step - loss: 0.8319 - accuracy: 0.6927
# Epoch 6/40
# 313/313 [==============================] - 0s 602us/step - loss: 0.7399 - accuracy: 0.7354
# Epoch 7/40
# 313/313 [==============================] - 0s 584us/step - loss: 0.6710 - accuracy: 0.7650
# Epoch 8/40
# 313/313 [==============================] - 0s 578us/step - loss: 0.6079 - accuracy: 0.7954
# Epoch 9/40
# 313/313 [==============================] - 0s 577us/step - loss: 0.5706 - accuracy: 0.8085
# Epoch 10/40
# 313/313 [==============================] - 0s 610us/step - loss: 0.5241 - accuracy: 0.8271
# Epoch 11/40
# 313/313 [==============================] - 0s 617us/step - loss: 0.4987 - accuracy: 0.8330
# Epoch 12/40
# 313/313 [==============================] - 0s 602us/step - loss: 0.4687 - accuracy: 0.8485
# Epoch 13/40
# 313/313 [==============================] - 0s 616us/step - loss: 0.4476 - accuracy: 0.8576
# Epoch 14/40
# 313/313 [==============================] - 0s 599us/step - loss: 0.4285 - accuracy: 0.8620
# Epoch 15/40
# 313/313 [==============================] - 0s 648us/step - loss: 0.4128 - accuracy: 0.8647
# Epoch 16/40
# 313/313 [==============================] - 0s 610us/step - loss: 0.3969 - accuracy: 0.8720
# Epoch 17/40
# 313/313 [==============================] - 0s 608us/step - loss: 0.3799 - accuracy: 0.8802
# Epoch 18/40
# 313/313 [==============================] - 0s 612us/step - loss: 0.3730 - accuracy: 0.8765
# Epoch 19/40
# 313/313 [==============================] - 0s 626us/step - loss: 0.3623 - accuracy: 0.8793
# Epoch 20/40
# 313/313 [==============================] - 0s 622us/step - loss: 0.3504 - accuracy: 0.8836
# Epoch 21/40
# 313/313 [==============================] - 0s 575us/step - loss: 0.3452 - accuracy: 0.8832
# Epoch 22/40
# 313/313 [==============================] - 0s 579us/step - loss: 0.3336 - accuracy: 0.8899
# Epoch 23/40
# 313/313 [==============================] - 0s 576us/step - loss: 0.3232 - accuracy: 0.8926
# Epoch 24/40
# 313/313 [==============================] - 0s 617us/step - loss: 0.3181 - accuracy: 0.8990
# Epoch 25/40
# 313/313 [==============================] - 0s 622us/step - loss: 0.3137 - accuracy: 0.8982
# Epoch 26/40
# 313/313 [==============================] - 0s 613us/step - loss: 0.3122 - accuracy: 0.8913
# Epoch 27/40
# 313/313 [==============================] - 0s 617us/step - loss: 0.3052 - accuracy: 0.8912
# Epoch 28/40
# 313/313 [==============================] - 0s 610us/step - loss: 0.2987 - accuracy: 0.8963
# Epoch 29/40
# 313/313 [==============================] - 0s 616us/step - loss: 0.2889 - accuracy: 0.9001
# Epoch 30/40
# 313/313 [==============================] - 0s 602us/step - loss: 0.2867 - accuracy: 0.9025
# Epoch 31/40
# 313/313 [==============================] - 0s 620us/step - loss: 0.2834 - accuracy: 0.9017
# Epoch 32/40
# 313/313 [==============================] - 0s 594us/step - loss: 0.2800 - accuracy: 0.8974
# Epoch 33/40
# 313/313 [==============================] - 0s 597us/step - loss: 0.2787 - accuracy: 0.9008
# Epoch 34/40
# 313/313 [==============================] - 0s 614us/step - loss: 0.2754 - accuracy: 0.9056
# Epoch 35/40
# 313/313 [==============================] - 0s 585us/step - loss: 0.2642 - accuracy: 0.9072
# Epoch 36/40
# 313/313 [==============================] - 0s 627us/step - loss: 0.2637 - accuracy: 0.9066
# Epoch 37/40
# 313/313 [==============================] - 0s 583us/step - loss: 0.2616 - accuracy: 0.9086
# Epoch 38/40
# 313/313 [==============================] - 0s 588us/step - loss: 0.2549 - accuracy: 0.9108
# Epoch 39/40
# 313/313 [==============================] - 0s 611us/step - loss: 0.2546 - accuracy: 0.9114
# Epoch 40/40
# 313/313 [==============================] - 0s 608us/step - loss: 0.2490 - accuracy: 0.9133
# [[0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00
# 2.3084011e-29 6.1135594e-20 7.5358608e-12 3.5170084e-05 9.9996483e-01]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment