The image shows the GUI:
I am making a GUI with Tkinter which can show graphs which have been plotted with matplotlib.
The graphs get their x and y values from a JSON file.
The data from the JSON file gets collected by some code which I wrote (this code is included in the code below)
What works: The collecting of data from the JSON-file, plotting the data in a graph via matplotlib, and showing this graph on a tkinter canvas all works fine.
Problem: I cannot clear the canvas, such that I can display another graph based on data from another JSON file. I have to close the program, start it again, and select a different JSON file, if I want to see another graph.
Here is the code:
################################### GUI program for graphs #######################################
##### Scrollbar for the listbox #####
Myframe = Frame(inspect_data)
Myframe.pack(side=tk.LEFT, fill=Y)
my_scrollbar = Scrollbar(Myframe, orient=VERTICAL)
#####Listbox#####
vores_listebox = Listbox(Myframe, width=22, height=40,
yscrollcommand=my_scrollbar.set)
vores_listebox.pack(side=LEFT, expand=True)
my_scrollbar.config(command=vores_listebox.yview)
my_scrollbar.pack(side=LEFT, fill=Y)
Myframe.pack()
###### Button functions ######
path_us = ""
def vis_NS():
vores_listebox.delete(0, END)
# Husk at ændre mapper##
global path_us
path_us = "C://Users//canal//OneDrive//Dokumenter//AAU//3. semester//1//"
for x in os.listdir(path_us):
if x.endswith(".json"):
vores_listebox.insert(END, x)
def vis_OS():
global path_us
path_us = "C://Users//canal//OneDrive//Dokumenter//AAU//3. semester//2//"
vores_listebox.delete(0, END)
# De NS;OS,US,MS skal ligge i hver sin mappe#
for x in os.listdir(path_us):
if x.endswith(".json"):
vores_listebox.insert(END, x)
def vis_US():
vores_listebox.delete(0, END)
for x in os.listdir("C://Users//canal//OneDrive//Dokumenter//AAU//3. semester//2"):
if x.endswith(".json"):
vores_listebox.insert(END, x)
def vis_MS():
vores_listebox.delete(0, END)
for x in os.listdir("C://Users//canal//OneDrive//Dokumenter//AAU//3. semester//2"):
if x.endswith(".json"):
vores_listebox.insert(END, x)
#### Buttons #####
button1 = Button(inspect_data,
text="Normal screwing",
width=17,
height=2,
bg="white",
fg="black",
command=vis_NS
)
button1.place(x=5, y=10)
button2 = Button(inspect_data,
text="Over screwing",
width=17,
height=2,
bg="white",
fg="black",
command=vis_OS
)
button2.place(x=5, y=50)
button3 = Button(inspect_data,
text="Under screwing",
width=17,
height=2,
bg="white",
fg="black",
command=vis_US
)
button3.place(x=5, y=90)
button4 = Button(inspect_data,
text="Missing screw",
width=17,
height=2,
bg="white",
fg="black",
command=vis_MS
)
button4.place(x=5, y=130)
######en fil bliver trykket#####
# placing the canvas on the Tkinter window
def items_selected(event):
selected_indices = vores_listebox.curselection()
selected_json = ",".join([vores_listebox.get(i) for i in selected_indices])
full_file_path = path_us + selected_json
open_json = js.load(open(full_file_path, "r"))
time = [open_json['XML_Data']
['Wsk3Vectors']['X_Axis']['Values']['float']]
rpm = [open_json['XML_Data']['Wsk3Vectors']
['Y_AxesList']['AxisData'][0]['Values']['float']]
torque = [open_json['XML_Data']['Wsk3Vectors']
['Y_AxesList']['AxisData'][1]['Values']['float']]
current = [open_json['XML_Data']['Wsk3Vectors']
['Y_AxesList']['AxisData'][2]['Values']['float']]
angle = [open_json['XML_Data']['Wsk3Vectors']
['Y_AxesList']['AxisData'][3]['Values']['float']]
depth = [open_json['XML_Data']['Wsk3Vectors']
['Y_AxesList']['AxisData'][4]['Values']['float']]
####### Using Matlib.pyplot to plot 5 graphs #######
plt.rcParams["figure.figsize"] = (7, 10)
plt.subplot(5, 1, 1)
plt.scatter(time, rpm, c="b", linewidths=2,
marker=",", edgecolor="b", s=1, alpha=0.5)
plt.title(selected_json)
plt.gca().axes.xaxis.set_ticklabels([])
plt.ylabel("RPM")
plt.grid()
plt.subplot(5, 1, 2)
plt.scatter(time, torque, c="g", linewidths=1,
marker=",", edgecolor="g", s=1, alpha=0.3)
plt.gca().axes.xaxis.set_ticklabels([])
plt.ylabel("Torque [Nm]")
plt.grid()
plt.subplot(5, 1, 3)
plt.scatter(time, current, c="r", linewidths=2,
marker=",", edgecolor="r", s=1, alpha=0.5)
plt.gca().axes.xaxis.set_ticklabels([])
plt.ylabel("Current [Amps]")
plt.grid()
plt.subplot(5, 1, 4)
plt.scatter(time, angle, c="m", linewidths=2,
marker=",", edgecolor="m", s=1, alpha=0.5)
plt.gca().axes.xaxis.set_ticklabels([])
plt.ylabel("Angle [RAD]")
plt.grid()
plt.subplot(5, 1, 5)
plt.scatter(time, depth, c="c", linewidths=2,
marker=",", edgecolor="c", s=1, alpha=0.5)
plt.xlabel("Time [ms]")
plt.ylabel("Depth [mm]")
plt.grid()
#### Sowing all the subplots in a tkinter canvas ########
fig = plt.figure()
canvas = FigureCanvasTkAgg(fig, master=inspect_data)
canvas.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# toolbar = matplotlib.NavigationToolbar2TkAgg(
# canvas, self)
# toolbar.update()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
vores_listebox.bind('<<ListboxSelect>>', items_selected)
root.after(1000, converter)
root.mainloop()
I have tried canvas.delete(all), plt.clf() among other things. One solution I think could work is:
If I start the function def items_selected to clear the figure inside the canvas by using: plt.clf(fig), however "fig" is not defined at this point, so python wouldn't know what this means.
it seems to me that the problem originates from plotting to plt, and not directly onto some axis from a figure linked to tkinter. Try to bind your subplot figure directly to your canvas outside the items_selected() function, and then redraw the canvas whenever you change the underlying matplotlib figure:
fig, axes = plt.subplots(5, 1, figsize=(7, 10))
canvas = FigureCanvasTkAgg(fig, inspect_data)
canvas.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
def items_selected(event):
.....
axes[0].scatter(time, rpm, c="b", linewidths=2,
marker=",", edgecolor="b", s=1, alpha=0.5)
axes[0].set_title(selected_json)
axes[0].set_xticklabels([])
axes[0].set_ylabel("RPM")
axes[0].grid()
....
canvas.draw()
Related
I am trying to create a Custom PyEnvironment for making a 2D grid (5*6 dimensions) to make the agent learn the optimum path from top left corner to bottom right corner.
I am facing this error :
ValueError: Expected q_network to emit a floating point tensor with inner dims (4,); but saw network output spec: TensorSpec(shape=(5, 4), dtype=tf.float32, name=None)
Below is the code of Environment class:
class MyEnvironment(py_environment.PyEnvironment):
def __init__(self):
super().__init__()
self._action_spec = array_spec.BoundedArraySpec(
shape=(), dtype=np.int32, name="action", minimum=0, maximum=3)
self._state_spec = array_spec.BoundedArraySpec(
shape=(5, 6), dtype=np.int32, name="observation", minimum=0, maximum=1)
self.discount = 0.99
def action_spec(self):
return self._action_spec
def state_spec(self):
return self._state_spec
def observation_spec(self):
return self._state_spec
def _reset(self):
self._state = np.zeros(2, dtype=np.int32)
obs = np.zeros((5, 6), dtype=np.int32)
obs[self._state[0], self._state[1]] = 1
return ts.restart(obs)
def _step(self, action):
self._state += [(-1, 0), (+1, 0), (0, -1), (0, +1)][action]
reward = 0
done = False
obs = np.zeros((5, 6), dtype=np.int32)
if(self._state[0]<0 or self._state[0]>4 or self._state[1]>5 or self._state[1]<0):
done = True
if not done:
obs[self._state[0], self._state[1]] = 1
if done or np.all(self._state == np.array([4,5])):
reward = -1 if done else +10
return ts.termination(obs, reward)
else:
return ts.transition(obs, reward, self.discount)
Qnetwork :
action_tensor_spec = tensor_spec.from_spec(env.action_spec())
num_actions = 4
# Define a helper function to create Dense layers configured with the right
# activation and kernel initializer.
def dense_layer(num_units):
return tf.keras.layers.Dense(
num_units,
activation=tf.keras.activations.relu,
kernel_initializer=tf.keras.initializers.VarianceScaling(
scale=2.0, mode='fan_in', distribution='truncated_normal'))
# QNetwork consists of a sequence of Dense layers followed by a dense layer
# with `num_actions` units to generate one q_value per available action as
# it's output.
dense_layers = [dense_layer(num_units) for num_units in fc_layer_params]
q_values_layer = tf.keras.layers.Dense(
num_actions,
activation=None,
kernel_initializer=tf.keras.initializers.RandomUniform(
minval=-0.03, maxval=0.03),
bias_initializer=tf.keras.initializers.Constant(-0.2))
q_net = sequential.Sequential(dense_layers + [q_values_layer])
**Error after running this : **
#ValueError: Expected q_network to emit a floating point tensor with inner dims (4,); but saw network output spec: TensorSpec(shape=(5, 4), dtype=tf.float32, name=None)
Hello I'm using Google colab to display multiplot that's why I tried to create one scatterplot with 3 scatter in it and under that, there is a parallel categories plot that should be connected to scatter but when I try to select a group of points in scatter, I cannot change color in parcat it's always grey, I think those functions such as on_click and on_selection does not work but I couldn't replace them with another code can someone help me?
import plotly.graph_objects as go
from ipywidgets import widgets
import pandas as pd
import numpy as np
# Build parcats dimensions
#here dfTot is my big class include every layer(input,1,2,3,output)
#Check image under the Code
dfTot = pd.concat([df1,df2,df3,df4], axis=1,join='inner')
categorical_dimensions = ['Layer Input', 'Layer 2', 'Layer 3','Layer Output'];
dimensions = [dict(values=dfTot[label], label=label) for label in categorical_dimensions]
# Build colorscale
color = np.zeros(len(dfTot), dtype='uint8')
colorscale = [[0, 'gray'], [1, 'firebrick']]
# Build figure as FigureWidget
fig = go.FigureWidget(
data=[
go.Scatter(x=layer_activation1[:,0], y=layer_activation1[:,1],showlegend = False,hovertemplate =y+"<br>"+"Cluster Groupe : "+df2['Layer 2']+"<br>"+"Layer 2"'<extra></extra>',
marker={'color': 'gray'}, mode='markers', selected={'marker': {'color': 'firebrick'}},
unselected={'marker': {'opacity': 0.3}}),
go.Scatter(x=layer_activation2[:,0], y=layer_activation2[:,1],showlegend = False,hovertemplate =y+"<br>"+"Cluster Groupe : "+df3['Layer 3']+"<br>"+"Layer 3"'<extra></extra>',
marker={'color': 'gray'}, mode='markers', selected={'marker': {'color': 'firebrick'}},
unselected={'marker': {'opacity': 0.3}}),
go.Scatter(x=layer_activation3[:,0], y=layer_activation3[:,1],showlegend = False,hovertemplate =y+"</br>"+"Cluster Groupe : "+df4['Layer Output']+"<br>"+"Layer Output"'<extra></extra>',
marker={'color': 'gray'}, mode='markers', selected={'marker': {'color': 'firebrick'}},
unselected={'marker': {'opacity': 0.3}}),
go.Parcats(
domain={'y': [0, 0.4]}, dimensions=dimensions,
line={'colorscale': colorscale, 'cmin': 0,
'cmax': 1, 'color': color, 'shape': 'hspline'})
])
fig.update_layout(
height=800, xaxis={'title': 'Axis x'},
yaxis={'title': 'Axis y', 'domain': [0.6, 1]},
dragmode='lasso', hovermode='closest')
# Update color callback
def update_color(trace, points, state):
new_color = np.zeros(len(dfTot), dtype='uint8')
# Update scatter selection
fig.data[0].selectedpoints = points.point_inds
new_color[points.point_inds] = 1
fig.data[3].line.color = new_color
fig.data[1].selectedpoints = points.point_inds
new_color[points.point_inds] = 1
fig.data[3].line.color = new_color
fig.data[2].selectedpoints = points.point_inds
# Update parcats colors
new_color[points.point_inds] = 1
fig.data[3].line.color = new_color
# Register callback on scatter selection...
fig.data[0].on_selection(update_color)
fig.data[1].on_selection(update_color)
fig.data[2].on_selection(update_color)
# and parcats click
fig.data[3].on_click(update_color)
Here is the screenshot of plot
def AdaIN(x):
#Normalize x[0] (image representation)
mean = K.mean(x[0], axis = [1, 2], keepdims = True)
std = K.std(x[0], axis = [1, 2], keepdims = True) + 1e-7
y = (x[0] - mean) / std
#Reshape scale and bias parameters
pool_shape = [-1, 1, 1, y.shape[-1]]
scale = K.reshape(x[1], pool_shape)
bias = K.reshape(x[2], pool_shape)#Multiply by x[1] (GAMMA) and add x[2] (BETA)
return y * scale + bias
def g_block(input_tensor, latent_vector, filters):
gamma = Dense(filters, bias_initializer = 'ones')(latent_vector)
beta = Dense(filters)(latent_vector)
out = UpSampling2D()(input_tensor)
out = Conv2D(filters, 3, padding = 'same')(out)
out = Lambda(AdaIN)([out, gamma, beta])
out = Activation('relu')(out)
return out
Please see code above. I am currently studying styleGAN. I am trying to convert this code into pytorch but I cant seem to understand what does Lambda do in g_block. AdaIN needs only one input based on its declaration but some how is gamma and beta also used as input? Please inform me what does the Lambda do in this code.
Thank you very much.
Lambda layers in keras are used to call custom functions inside the model. In g_block Lambda calls AdaIN function and passes out, gamma, beta as arguments inside a list. And AdaIN function receives these 3 tensors encapsulated within a single list as x. And also those tensors are accessed inside AdaIN function by indexing list x(x[0], x[1], x[2]).
Here's pytorch equivalent:
import torch
import torch.nn as nn
import torch.nn.functional as F
class AdaIN(nn.Module):
def forward(self, out, gamma, beta):
bs, ch = out.size()[:2]
mean = out.reshape(bs, ch, -1).mean(dim=2).reshape(bs, ch, 1, 1)
std = out.reshape(bs, ch, -1).std(dim=2).reshape(bs, ch, 1, 1) + 1e-7
y = (out - mean) / std
bias = beta.unsqueeze(-1).unsqueeze(-1).expand_as(out)
scale = gamma.unsqueeze(-1).unsqueeze(-1).expand_as(out)
return y * scale + bias
class g_block(nn.Module):
def __init__(self, filters, latent_vector_shape, input_tensor_channels):
super().__init__()
self.gamma = nn.Linear(in_features = latent_vector_shape, out_features = filters)
# Initializes all bias to 1
self.gamma.bias.data = torch.ones(filters)
self.beta = nn.Linear(in_features = latent_vector_shape, out_features = filters)
# calculate appropriate padding
self.conv = nn.Conv2d(input_tensor_channels, filters, 3, 1, padding=1)# calc padding
self.adain = AdaIN()
def forward(self, input_tensor, latent_vector):
gamma = self.gamma(latent_vector)
beta = self.beta(latent_vector)
# check default interpolation mode in keras and replace mode below if different
out = F.interpolate(input_tensor, scale_factor=2, mode='nearest')
out = self.conv(out)
out = self.adain(out, gamma, beta)
out = torch.relu(out)
return out
# Sample:
input_tensor = torch.randn((1, 3, 10, 10))
latent_vector = torch.randn((1, 5))
g = g_block(3, latent_vector.shape[1], input_tensor.shape[1])
out = g(input_tensor, latent_vector)
print(out)
Note: you need to pass latent_vector and input_tensor shapes while creating g_block.
I have a small animation code that is built using PyGame (and Python 2.7). I am able to build the executable folder with Pyinstaller, however when I try to run the executable it is crashing, although I am able to run the same files (i.e. source code) through my terminal without any problem.
import pygame
import exercise
import random as rd
import copy
import pandas as pd
import numpy as np
def animate():
debug_mode=False
done1 = False
dc_x = 30
dc_y = 30
plant_x = 110
plant_y = 30
supp_x = 190
supp_y = 30
WHITE = (255, 255, 255)
BLACK = (0, 0, 0) #arrows
RED = (255, 0, 0) # Box frames for summary of t, cost and lost sales
GREEN = (0, 255, 0) # Box frames for shipment amounts
DEEPSKYBLUE = (0,191,255) # arc to represent backup WIP inv
DOGERBLUE = (0,191,255) # arc to represent backup FG inv
INDIGO = (75,0,130) # DC/Supp up state
LAVENDER=(238, 130, 238) #DC/Supp down state
OLIVE=(128,128,0) #plant down state
YELLOWGREEN=(173,255,47) #plant up state
DARKYELLOW=(204,204,0)
pygame.init()
screen = pygame.display.set_mode((1000, 600))
clock = pygame.time.Clock()
grand_record={}
############ READ AND PREPARE INITIAL SCREEN for t==0 ################
PLANT_status, SUPP_status, DC_status, disruption, DC_BUp_Plan, Plant_BUp_Plan, Supp_BUp_Plan, Demand, P=exercise.init(debug_mode,False)
grand_record[0]={"DC":DC_status, "Plant":PLANT_status, "Supplier":SUPP_status}
basicFont = pygame.font.SysFont(None, 24)
largeFont = pygame.font.SysFont(None, 35)
pygame.display.set_caption('SCREAM!')
if debug_mode: R=1
else: R= rd.randint(0,25)
scenario_score={"cost":0, "service":0}
while not done1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done1 = True
#if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_DOWN:
# paused()
end_of_one_run=1
total_lost_customer=0
cumulative_inv_holding_cost=0
total_fixed_cost_paid=0
table_output=[]
chart_output=[]
header=["Week","Demand","Upstream shipment", "Shipment from regular DC", "Shipment from backup DC",
"FG Inv at Reg. DC", "FG Inv at B/up. DC", "FG Inv as B/up","LostSales","Start Order for b/up DC", "B/up DC starts","DC Disruption at","Disruption duration", "TotalAvailableWIPSupplyCap",
"Start Order for b/up Supplier", "B/up Supplier starts","Supplier Disruption at","Disruption duration", "Storage Cap Available", "Plant production",
"WIP Inv as B/up","Start Order for b/up Plant", "B/up Plant starts","Plant Disruption at","Disruption duration"]
table_output.append(header)
for t in range(1,P+1): # for the specified number of periods
screen.fill((0, 0, 0))
picture = pygame.image.load('Background.png').convert() #MITwallpaper
screen.blit(picture, [0, 0])
#import pdb; pdb.set_trace()
################################## MAKE CALCULATIONS FOR t>0 ################
grand_record,output=exercise.calculate(t, grand_record[t-1]["Plant"], grand_record[t-1]["Supplier"], grand_record[t-1]["DC"], disruption, DC_BUp_Plan, Plant_BUp_Plan, Supp_BUp_Plan,Demand[R*P+t],grand_record,debug_mode)
chart_output=exercise.prepare_chart_output(chart_output, grand_record)
output_loc=copy.deepcopy(output)
table_output.append(output_loc)
end_of_one_run=end_of_one_run+1
objectives=exercise.calculate_objectives(grand_record, DC_BUp_Plan, Plant_BUp_Plan, Supp_BUp_Plan, t)
lost_cust_text = largeFont.render("Service: "+str(objectives["Service"]), True, WHITE, BLACK)
lost_cust_textRect = lost_cust_text.get_rect()
lost_cust_textRect.centerx = 380
lost_cust_textRect.centery = 420
pygame.draw.rect(screen, RED, (lost_cust_textRect.left - 3, lost_cust_textRect.top - 3, lost_cust_textRect.width + 3, lost_cust_textRect.height + 3))
screen.blit(lost_cust_text, lost_cust_textRect)
inv_hold_text = largeFont.render("Inv.Holding Cost(k$): "+str(round(objectives["InvHoldingCost"]*0.001,1)), True, WHITE, BLACK)
inv_hold_textRect = inv_hold_text.get_rect()
inv_hold_textRect.centerx = 600
inv_hold_textRect.centery = 445
pygame.draw.rect(screen, RED, (inv_hold_textRect.left - 3, inv_hold_textRect.top - 3, inv_hold_textRect.width + 3, inv_hold_textRect.height + 3))
screen.blit(inv_hold_text, inv_hold_textRect)
startup_text = largeFont.render("Startup Cost(k$): "+str(round(objectives["Startup_cost"]*0.001,1)), True, WHITE, BLACK)
startup_textRect = startup_text.get_rect()
startup_textRect.centerx = 840
startup_textRect.centery = 470
pygame.draw.rect(screen, RED, (startup_textRect.left - 3, startup_textRect.top - 3, startup_textRect.width + 3, startup_textRect.height + 3))
screen.blit(startup_text, startup_textRect)
###################################################################################
if t<P:
pygame.display.flip()
clock.tick(8)
else:
#import pdb; pdb.set_trace()
table = pd.DataFrame(table_output)
table.to_excel("Details.xlsx",index=False)
#import pdb; pdb.set_trace()
done1 = True
################################ Want A PARETO Button ###################################
x=500
y=500
w=200
h=50
pygame.draw.rect(screen, DEEPSKYBLUE,(x,y,w,h))
buttontext = basicFont.render("Want A Pareto Solution?", True, BLACK , WHITE )
buttontext_rect=buttontext.get_rect()
buttontext_rect.centerx = (x+(w/2))
buttontext_rect.centery = (y+(h/2))
screen.blit(buttontext, buttontext_rect)
pygame.display.flip()
pygame.time.delay(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exercise.plotcharts(chart_output)
################################################################################
animate()
I had the same problem (also Python 2.7), and this did it for me:
pyinstaller usually has trouble with pygame.font.SysFont("some_font", 24)
Instead use pygame.font.Font("your_own_font.ttf", 24).
Download a font and either put it in the same folder as your file, or in a data folder.
I use VeraMono and put it in a data folder, so my code would be
...
basicFont = pygame.font.Font("data/VeraMono", 24)
largeFont = pygame.font.Font("data/VeraMono", 35)
pygame.display.set_caption('SCREAM!')
...
If you don't have a data/ folder added to your project yet, then add the following lines to your spec file (the spec file is created as soon as you run you pyinstaller the first time. It has the same name as your python file.)
# -*- mode: python -*-
block_cipher = None
# This entry is new!
added_files = [
( 'data', 'data'),
]
a = Analysis(['CleanTest.py'],
pathex=['/home/angela/Desktop/Arbeit/VisualTest'],
binaries=[],
datas=added_files, # add the new file to your data tree
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
...
Run pyinstaller <your_file>.spec, and hopefully it'll work.
my guess is that it crashes when trying to load the background image:
picture = pygame.image.load('Background.png').convert() #MITwallpaper
When pyinstaller compiles your code, that is all it compiles (including the imported dependencies). It creates the dist directory where you can run your executable and you are good to go.
But, by default, pyinstaller will not pull in external resources. So when you run the executable, it can't find your image because it is in a different directory. You can test this by manually copying the image to the folder where the exe is located and see if it works.
Also, you can run the exe and push the output into a debug file:
your_file.exe > debug.txt
This way you can capture the output from your program (including error messages).
I want to create a simple autoencoder with 3000 input, 2 hidden and 3000 output neurons:
def build_autoencoder(input_var=None):
l_in = InputLayer(shape=(None,3000), input_var=input_var)
l_hid = DenseLayer(
l_in, num_units=2,
nonlinearity=rectify,
W=lasagne.init.GlorotUniform())
l_out = DenseLayer(
l_hid, num_units=3000,
nonlinearity=softmax)
return l_out
The shape of the training data is as follows:
train.shape = (3000,3)
This is input, target and loss function definition:
import sys
import os
import time
import numpy as np
import theano
import theano.tensor as T
import lasagne
from lasagne.updates import rmsprop
from lasagne.layers import DenseLayer, DropoutLayer, InputLayer
from lasagne.nonlinearities import rectify, softmax
from lasagne.objectives import categorical_crossentropy
# Creating the Theano variables
input_var = T.dmatrix('inputs')
target_var = T.dmatrix('targets')
# Building the Theano expressions on these variables
network = build_autoencoder(input_var)
prediction = lasagne.layers.get_output(network)
loss = categorical_crossentropy(prediction, target_var)
loss = loss.mean()
test_prediction = lasagne.layers.get_output(network,
deterministic=True)
test_loss = categorical_crossentropy(test_prediction, target_var)
test_loss = test_loss.mean()
test_acc = T.mean(T.eq(T.argmax(test_prediction, axis=1), target_var),
dtype=theano.config.floatX)
I'm just running one epoch but get an error:
params = lasagne.layers.get_all_params(network, trainable=True)
updates = rmsprop(loss, params, learning_rate=0.001)
# Compiling the graph by declaring the Theano functions
train_fn = theano.function([input_var, target_var],
loss, updates=updates)
val_fn = theano.function([input_var, target_var],
[test_loss, test_acc])
# For loop that goes each time through the hole training
# and validation data
print("Starting training...")
for epoch in range(1):
# Going over the training data
train_err = 0
train_batches = 0
start_time = time.time()
print 'test1'
train_err += train_fn(train, train)
train_batches += 1
# Going over the validation data
val_err = 0
val_acc = 0
val_batches = 0
err, acc = val_fn(train, train)
val_err += err
val_acc += acc
val_batches += 1
# Then we print the results for this epoch:
print("Epoch {} of {} took {:.3f}s".format(epoch + 1, num_epochs, time.time() - start_time))
print("training loss:\t\t{:.6f}".format(train_err / train_batches))
print("validation loss:\t\t{:.6f}".format(val_err / val_batches))
print("validation accuracy:\t\t{:.2f} %".format(val_acc / val_batches * 100))
This is the error:
ValueError: ('shapes (3000,3) and (3000,2) not aligned: 3 (dim 1) !=
3000 (dim 0)', (3000, 3), (3000, 2)) Apply node that caused the error:
Dot22(inputs, W) Toposort index: 3 Inputs types: [TensorType(float64,
matrix), TensorType(float64, matrix)] Inputs shapes: [(3000, 3),
(3000, 2)] Inputs strides: [(24, 8), (16, 8)] Inputs values: ['not
shown', 'not shown'] Outputs clients:
[[Elemwise{add,no_inplace}(Dot22.0, InplaceDimShuffle{x,0}.0),
Elemwise{Composite{(i0 * (Abs(i1) + i2 + i3))}}[(0,
2)](TensorConstant{(1, 1) of 0.5}, Elemwise{add,no_inplace}.0,
Dot22.0, InplaceDimShuffle{x,0}.0)]]
To me it seems that the bottleneck of the auto encoder is the problem. Any ideas?
Just got some help from my IBM college (Erwan), I've posted the working solution to this GIST, the relevant sections are these ones:
First, get the shape of the training data correct:
train.shape = (3, 3000)
Then use the same shape on the InputLayer:
def build_autoencoder(input_var=None):
l_in = InputLayer(shape=(3, 3000), input_var=input_var)
l_hid = DenseLayer(
l_in, num_units=2,
nonlinearity=rectify,
W=lasagne.init.GlorotUniform())
l_out = DenseLayer(
l_hid, num_units=3000,
nonlinearity=softmax)
return l_out
So this is solved, next problem is getting a descending cost during training, but this is another topic :)