Using a function to give out all the information that was given - function

converting dollars to euros
# 0.92 euros equals 1.00 dollar
def currency_exchange(*dollars):
try:
euros = float(dollars * 0.92)
except TypeError:
print("The amount in euros is {:.2f}".format(euros), end="")
except KeyError:
print(euros)
except UnboundLocalError:
print(euros)
return euros
print(currency_exchange(3, 4, 5))
My goal here is converting dollars to euros. I want all the information to be shown. It only gives the number and not the print statement with the formatting in place. It shows too many errors.
I tried this. But it won't format to two decimal places
def currency_exchange(*dollars):
euros = float(dollars * 0.92)
print("The amount in euros is {:.2f}".format(euros), end="")
return euros
print(currency_exchange(3, 4, 5))

Related

Do linear layer after GRU saved the sequence output order?

I'm dealing with the following senario:
My input has the shape of: [batch_size, input_sequence_length, input_features]
where:
input_sequence_length = 10
input_features = 3
My output has the shape of: [batch_size, output_sequence_length]
where:
output_sequence_length = 5
i.e: for each time slot of 10 units (each slot with 3 features) I need to predict the next 5 slots values.
I built the following model:
import torch
import torch.nn as nn
import torchinfo
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.GRU = nn.GRU(input_size=3, hidden_size=32, num_layers=2, batch_first=True)
self.fc = nn.Linear(32, 5)
def forward(self, input_series):
output, h = self.GRU(input_series)
output = output[:, -1, :] # get last state
output = self.fc(output)
output = output.view(-1, 5, 1) # reorginize output
return output
torchinfo.summary(MyModel(), (512, 10, 3))
==========================================================================================
Layer (type:depth-idx) Output Shape Param #
==========================================================================================
MyModel [512, 5, 1] --
├─GRU: 1-1 [512, 10, 32] 9,888
├─Linear: 1-2 [512, 5] 165
==========================================================================================
I'm getting good results (very small MSE loss, and the predictions looks good),
but I'm not sure if the model output (5 sequence values) are really ordered by the model ?
i.e the second output based on the first output and the third output based on the second output ...
I know that the GRU output based on the learned sequence history.
But I'm also used linear layer, so is the output (after the linear layer) still sorted by time ?
UPDATE
This answer isn't quite right, see this follow-up question. The best way is to write the math and show that the 5 scalar outputs aren't functions of each other.
Old Answer
I'm not sure if the model output (5 sequence values) are really ordered by the model ? i.e the second output based on the first output and the third output based on the second output
No, they aren't. You can check that the gradients of, say, the last output w.r.t to the previous outputs are zeroes, which basically means that the last output isn't a function of the previous outputs.
model = MyModel()
x = torch.rand([2, 10, 3])
y = model(x)
y.retain_grad() # allows accessing y.grad although y is a non-leaf Tensor
y[:, -1].sum().backward() # computes gradients of last output
assert torch.allclose(y.grad[:, :-1], torch.tensor(0.)) # gradients w.r.t previous outputs are zeroes
A popular model to capture dependencies among output labels is conditional random fields. But since you're already happy with the predictions of the current model, perhaps modelling the output dependencies isn't that important.

how can i make the input work with my parameters?

I have started a python class and my book does not seem to help me.
My professor has a program that bombards my code with different inputs and if any of the inputs do not work then my code is "wrong". I have done many days worth of editing and am at a complete loss. I have the code working if someone puts and input of an actual number. But where my code fails the test is if input is "miles_to_laps(26)" it errors out.
I have tried changing the input to int(input()) but that does not fix the issue. I've gone through changing variables and even changing the input method but still am at a loss. I have already tried contacting my teacher but 6 days of no response and 3 days of being late i feel like I'm just going no where.
user_miles = int(input())
def miles_to_laps(user_miles):
x = user_miles
y = 4
x2 = x * y
result = print('%0.2f' % float(x2))
return result
miles_to_laps(user_miles)
my code works for real number inputs but my professor is wanting inputs like
miles_to_laps(26) and miles_to_laps(13) to create the same outputs.
For the wierd input functionality you can try:
import re
def parse_function_text(s):
try:
return re.search("miles_to_laps\((.+)\)", s)[1]
except TypeError:
return None
def accept_input(user_input):
desugar = parse_function_text(user_input)
if desugar is not None:
user_input = desugar
try:
return float(user_input)
except ValueError:
raise ValueError("Cannot process input %s" % user_input)
assert accept_input("miles_to_laps(3.5)") == 3.5
I'm trying to keep all the pedantism aside, but what kind of CS/programming teaching is that?
Areas of concern:
separate user input from rest of code
separate output formatting from function output
the code inside miles_to_laps is excessive
Now here is the code to try:
LAPS_PER_MILE = 4
# the only calculation, "pure" function
def miles_to_laps(miles):
return LAPS_PER_MILE * miles
# sorting out valid vs invalid input, "interface"
def accept_input(user_input):
try:
return float(user_input)
except ValueError:
raise ValueError("Cannot process input %s" % user_input)
if __name__ == "__main__":
# running the program
laps = miles_to_laps(accept_input(input()))
print ('%0.2f' % laps)
Hope this is not too overwhelming.
Update: second attempt
MILE = 1609.34 # meters per mile
LAP = 400 # track lap
LAPS_PER_MILE = MILE/LAP
def miles_to_laps(miles):
return LAPS_PER_MILE * miles
def laps_coerced(laps):
return '%0.2f' % laps
def accept_input(user_input):
try:
return float(user_input)
except ValueError:
raise ValueError("Cannot process input %s" % user_input)
def main(user_input_str):
miles = accept_input(user_input_str)
laps = miles_to_laps(miles)
print (laps_coerced(laps))
if __name__ == "__main__":
main(input())

Classification of string of characters (modified)

I am working on a problem where I have some 32514 rows of jumbled characters "wewlsfnskfddsl...eredsda" and each row is of length 406 chars. We need to predict which class do they belong to? Here class is 1-12 names of books.
After searching around in the internet I tried the following. Yet, I get a error. Thank you so much.
#code
y = ytrain.values
#ytrain = y.ravel()
y = to_categorical(y, num_classes=12)
[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
...
[0. 0. 0. ... 0. 1. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]]
X = X.reshape((1,32514,1))
# define model
model = Sequential()
model.add(LSTM(75, input_shape=(32514,1)))
model.add(Dense(12, activation='softmax'))
print(model.summary())
# compile model
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit model
model.fit(X, y, epochs=100, verbose=2)
# save the model to file
model.save('model.h5')
# save the mapping
dump(mapping, open('mapping.pkl', 'wb'))
#(batch_size, input_dim)
#(batch_size, timesteps, input_dim)
#### I get the following error:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_19 (LSTM) (None, 75) 23100
_________________________________________________________________
dense_13 (Dense) (None, 12) 912
=================================================================
Total params: 24,012
Trainable params: 24,012
Non-trainable params: 0
_________________________________________________________________
None
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-35-503a6273e5d0> in <module>()
7
8 # fit model
----> 9 model.fit(X, y, epochs=100, verbose=2)
10
11 # save the model to file
/usr/local/lib/python3.6/dist-packages/keras/models.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, **kwargs)
1000 initial_epoch=initial_epoch,
1001 steps_per_epoch=steps_per_epoch,
-> 1002 validation_steps=validation_steps)
1003
1004 def evaluate(self, x=None, y=None,
/usr/local/lib/python3.6/dist-packages/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, **kwargs)
1628 sample_weight=sample_weight,
1629 class_weight=class_weight,
-> 1630 batch_size=batch_size)
1631 # Prepare validation data.
1632 do_validation = False
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
1478 output_shapes,
1479 check_batch_axis=False,
-> 1480 exception_prefix='target')
1481 sample_weights = _standardize_sample_weights(sample_weight,
1482 self._feed_output_names)
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
121 ': expected ' + names[i] + ' to have shape ' +
122 str(shape) + ' but got array with shape ' +
--> 123 str(data_shape))
124 return data
125
ValueError: Error when checking target: expected dense_13 to have shape (1,) but got array with shape (12,)
Machine learning & deep learning models for text classification are complex to construct. Here is a guide that can help you get started.
https://papers.nips.cc/paper/5782-character-level-convolutional-networks-for-text-classification.pdf
Hope it helps! :-)
It seems to me that you can tackle this problem using lstm.
Long short-term memory (LSTM) units (or blocks) are a building unit for layers of a recurrent neural network (RNN)
These LSTM will help us to capture sequential information and generally used in case where we want to learn the sequential patterns in the data
You can decode this problem using character level LSTM.
In this you have to pass every character of the text in a LSTM cell.and at the last time step you will have a class which is the true label
You can use cross-entropy loss function.
https://machinelearningmastery.com/develop-character-based-neural-language-model-keras/
This will give you complete idea

ValueError: Shape mismatch: when using Keras Autoencoder(Theano Backend)

My data is a (83,104) pandas dataframe. And my first(only) hidden layer is expected to have 5 neurons.
When I wrote my code like this:
input_img = Input(shape=(104,)) # 104 dates as variable
encoded = Dense(5, activation='relu')(input_img)
decoded = Dense(104, activation='relu')(encoded)
autoencoder = Model(input_img, decoded)
autoencoder.compile(loss='mean_squared_error', optimizer='sgd')
autoencoder.fit(train_data, train_data, epochs=50)
I receive error message:
ValueError: Shape mismatch: x has 32 cols (and 83 rows) but y has 104 rows (and 5 cols)
Apply node that caused the error: Dot22(/input_24, dense_47/kernel)
Toposort index: 2
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(83, 32), (104, 5)]
Inputs strides: [(4, 332), (20, 4)]
I have followed the guidance from https://blog.keras.io/building-autoencoders-in-keras.html.
Does anyone know what is wrong here?
Thanks,

is there an easy way to estimate size of a json object?

I'm working on a security service that will return a list of permissions and I'm trying to estimate the size of the json response object. Here's a piece of sample data:
ID=123
VariableName=CanAccessSomeContent
I'm looking for an easy way to estimate what size of the json response object will be with 1500 rows. Is there an online estimation tool or some other technique I can use to easily get a rough size estimate?
Using Python you can estimate the size by creating the dictionary or just make one...
import json
import os
import sys
dict = {}
for a in range(0, 1500):
dict[a] = {'VariableName': 'CanAccessSomeContent'}
output = json.dumps(dict, indent = 4)
print ("Estimated size: " + str(sys.getsizeof(output) / 1024) + "KB")
with open( "test.json", 'wb') as outfile:
outfile.write(output)
print ("Actual size: " + str(os.path.getsize('test.json') / 1024) + "KB")
Output:
Estimated size: 100KB
Actual size: 99KB
I solved it, when I needed to, by adding a File-like object that just counted the characters and json.dump()ing into it:
# File-like object, throws away everything you write to it but keeps track of the size.
class MeterFile:
def __init__(self, size=0):
self.size = size
def write(self, string):
self.size += len(string)
# Calculates the JSON-encoded size of an object without storing it.
def json_size(obj, *args, **kwargs):
mf = MeterFile()
json.dump(obj, mf, *args, **kwargs)
return mf.size
The advantage is that encoding is not stored in memory, which could be large especially in cases you care about the size to begin with.
Function to estimate file size (Mash of JSON-Size & UTF-8 Length node repos)
function json_filesize (value) {
// returns object size in bytes
return (~-encodeURI(JSON.stringify(value)).split(/%..|./).length)/1048576
}
json_filesize({foo: 'bar'}) >> 13
I'm not sure if this is what you're after, as this seems extremely basic, but here goes:
First start with 0 rows, encode it and measure the size (We'll call this A).
Then, get a decent sample from your database and encode 1 row at a time.
For each of those outputs, calculate the size and then store the average (we'll call this B).
Now for X rows, the estimated json response will be X * (B-A) + A
So if A was 100 bytes, and B was 150 bytes, for 1500 rows we'll get:
1500 * (150-100) + 100 = 75100 bytes = 73 KB