How can I convert MLMultiArray to words and words to MLMultiArray during prediction with MLModel file
I have a MLModel which is of type neural network. It accepts the input as MLMultiArray 1x3 and outputs the MLMultiArray. This is for the words prediction based on a single or set of words. Eg: I passed say three words and it predicted some words. I have seen the output in Jupiter notebook. But here in Xcode how can I convert a word or set of words first to a mlmultiarray and pass it to the predict function and then the output which is again a multiarray to the readable words. I have gone through all the examples but they are for the images. Any help will be appreciated
See also questions close to this topic
-
Free FMX component in Android / iOS
There are a lot of discussion about how to free component in mobile device with Firemonkey, but that not response to my answer.
With 10.4 RAD Studio version, Embarcadero unified memory management.
I create a TFrame with :
FFrame := TMyFrame.Create(nil); FFrame.OnClickCloseButton := CloseFrame;
On my CloseFrame method I do :
FFrame.DisposeOf; FFrame := nil;
It's ok on Android, but on iOS I got a Access Violation. Normally with 10.4 version of the IDE, I can do :
FFrame.Free;
But I got application crash in iOS.
What I need to do is really the most simple stuff. Create something and free it.
What do you recommend ? Use interface with a IFrame for example ?
-
iOS swift post реез request with binary body
I need to send an array of bytes to the server and receive another array of bytes in response, but the response comes
Status Code: 400, Headers { "Content-Type" = ( "text / html; charset = UTF-8" ); }
my code
let byteArray: [UInt8] = [UInt8](arrayLiteral: 0x05, 0x00, 0x07, 0x04, 0x00) let arrData = Data(byteArray) let requestURL = URL(string: "http://192.168.4.1/")! var request = URLRequest(url: requestURL) request.httpMethod = "POST" let session = URLSession(configuration: URLSessionConfiguration.default) request.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type") request.addValue(String(arrData.count), forHTTPHeaderField: "Content-Length") request.httpBody = arrData var task = session.dataTask(with: request){ data, response, error in print("DATA \(data ?? Data()) RESPONSE \(response) ERROR \(error)") } task.resume()
-
FBAudience network only showing test ad (Demo ad)
I have integrated FBAudienceNetwork in iOS for native ad. It works fine for test ad. When I addTestDevice with deviceHash it shows demo ad.
But when I don't add the device and clearTestDevices, it does not show any add, it gives an error: Ad can't be loaded: No-fill.
Can you please let me know what I have to do to show real ads?
Here is the code:
private func addTestDevicesForFacebookAds(){ FBAdSettings.isTestMode() let key = FBAdSettings.testDeviceHash() FBAdSettings.setLogLevel(FBAdLogLevel.log) FBAdSettings.addTestDevice(key) FBAdSettings.testAdType = .vid_HD_16_9_15s_Link } private func clearTestDevicesForFacebookAds() { FBAdSettings.clearTestDevices() }
-
How to extract the result of json with Codable
I'm using Codable for the first time and want to output the json result of Google Places details as a label. However, when I print it, the console says "The data could n’t be read because it isn’t in the correct format.”. I can't solve it by myself, so please tell me how to write it correctly.
Thanks.
The result of json
{ "html_attributions": [], "result": { "formatted_phone_number": "XXXX-XXX-XXX", "website": "https://www.xxxxx.com/xxxxxx/" }, "status": "OK" }
Detail.swift
import Foundation struct Details : Codable { var formatted_phone_number : String! var website : String! }
ViewController.swift
override func viewDidLoad() { super.viewDidLoad() fetchDetailData {(details) in for detail in details{ print(detail.website) } } } func fetchDetailData(completionHandler: @escaping ([Details]) -> Void){ let url = URL(string: "https://maps.googleapis.com/maps/api/place/details/json?place_id=\(place_id)&fields=formatted_phone_number,website&key=\(apikey)")! let task = URLSession.shared.dataTask(with: url){ (data,respose, error)in guard let data = data else{ return } do { let detailsData = try JSONDecoder().decode([Details].self, from: data) completionHandler(detailsData) } catch{ let error = error print(error.localizedDescription) } }.resume() }
-
How to find out database performance metrics of firebase firestore, aws dynamo db (amplify)
Hey I am currently working on a performance evaluation for Firebase Firestore but also for AWS Amplify (GraphQL) using an iOS-App, where I am currently measuring the response time of different requests (read/write/update/delete).
But my problem is that I can't really measure the network latency to firestore as well how long firestore itself takes to finish e.g. a write process to make it comparable to other databases.
I've already included the Performance SDK and enabled logging in my app. What I was able to find out is the commit timestamp from Firestore, but not the timestamp, when e.g. the write request was received and also not how big the network latency is, as I have to consider these in my evaluation.
Are there any ways to get these values (measuring, calculating etc.)?
I would be really grateful for some ideas especially for firebase but also AWS Amplify.
-
How to get better predictions by data augmentation on CNN model for Image Classification
I’m a beginner. I try Image Classification to my photos by using CNN model for on MNIST. To get better predictions, I did data augmentation to this, but I couldn’t improve it. Something wrong? And I don’t understand why CASE A on the following table had the wrong prediction to the number “4” comparing with CASE B, even though it has almost same accuracy at each time. Please give me some advice.
my 10 photos (.png, 28x28pixel)
- Change the number of MNIST data to 5 cases
- Data augmentation No or Yes to 5 cases
- Predict my 10 photos by using learned model
# import library from tensorflow import keras from tensorflow.keras import datasets, layers, models from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D from tensorflow.keras.models import Sequential from tensorflow.keras.preprocessing.image import ImageDataGenerator # MNIST mnist=keras.datasets.mnist (x_train,y_train),(x_test,y_test)=mnist.load_data() #(x_train,y_train),(x_test,y_test)=(x_train[:80],y_train[:80]),(x_test[:20], y_test[:20]) #(x_train,y_train),(x_test,y_test)=(x_train[:160],y_train[:160]),(x_test[:40], y_test[:40]) #(x_train,y_train),(x_test,y_test)=(x_train[:800],y_train[:800]),(x_test[:200], y_test[:200]) #(x_train,y_train),(x_test,y_test)=(x_train[:8000],y_train[:8000]),(x_test[:2000], y_test[:2000]) x_train=x_train.reshape(x_train.shape[0],28,28,1) x_test=x_test.reshape(x_test.shape[0],28,28,1) x_train=x_train/255 x_test=x_test/255 print("x_train",x_train.shape) print("x_test",x_test.shape) # Convolutional Neural Networks model = Sequential() model.add(Conv2D(16,(3,3),padding='same',input_shape=(28,28,1),activation='relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Conv2D(128,(3,3),activation='relu')) model.add(Conv2D(256,(3,3),activation='relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Dropout(0.5)) model.add(Flatten()) model.add(Dense(128,activation='relu')) model.add(Dropout(0.25)) model.add(Dense(10,activation='softmax')) model.summary() # model compile and learn model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(x_train,y_train,epochs=5) # evoluate for test data loss,acc=model.evaluate(x_test,y_test,verbose=2) print('accuracy:',acc) # data augmentation datagen=ImageDataGenerator(rescale=1/255, width_shift_range=0.01, height_shift_range=0.025, zoom_range=0.05) # learn history=model.fit_generator( datagen.flow(x_train,y_train,batch_size=64), steps_per_epoch=60, epochs=40, validation_data=(x_test,y_test), validation_steps=5, verbose=1) # evoluate for test data loss,acc=model.evaluate(x_test,y_test,verbose=2) print('accuracy:',acc)
-
Watson Visual Recognition alternative
Is there an alternative to Watson Visual Recognition that is easy to implement like this service? What made this API special was how easy it is to use for developers and designers without experience in machine learning or that it is difficult for us to train our own model. It was also quite generous in its Lite plan for enthusiasts that we only used it on personal projects.
-
Machine learning Prediction - Failed to convert a NumPy array to a Tensor
I'm having a movie rating prediction problem that I'm trying to solve for my personal machine learning practice. I have 2 csv files. One with movies(movieId, title, genres) and one with ratings(userId, movieId,rating, timestamp).
After doing some data preprocessing, apply word embeddings for movie titles and one-hot encoding for genres and shuffle my final dataframe Ι came up to this
userId movieId rating embeddings genres 0 545 2020 5.0 [0.081246674, 0.046522498, -0.014943261, 0.025... [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ... 1 427 3186 2.0 [0.09334839, 0.057055157, -0.020527517, 0.0301... [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ... 2 102 2144 3.0 [0.062349755, 0.04466611, -0.011009981, 0.0187... [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... 3 30 5927 4.0 [0.18021354, 0.119208135, -0.036116328, 0.0466... [0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, ... 4 537 1022 3.0 [0.026805451, 0.025356086, -0.004603084, 0.013... [0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, ... ... ... ... ... ... ...
After that I tried to split my data to X and y and apply train-test-split
X = df_ratings.drop(['rating'], axis=1).values y = df_ratings['rating'].values
Tried to :
X = np.asarray(X).astype('float32') y = np.asarray(y).astype('float32') TypeError Traceback (most recent call last) TypeError: only size-1 arrays can be converted to Python scalars The above exception was the direct cause of the following exception: ValueError Traceback (most recent call last) <ipython-input-46-ee1241369db7> in <module> ----> 1 X = np.asarray(X).astype('float32') 2 y = np.asarray(y).astype('float32') ValueError: setting an array element with a sequence
from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 0)
I created my model (I don't know if this is fully correct) and tried to fit my train-data to the model
def movies_model(): model = Sequential() # Add layers model.add(Dense(512, input_dim = X_train.shape[1], activation='relu')) model.add(Dense(512, input_dim = X_train.shape[1], activation='relu')) model.add(Dense(256, activation='relu')) model.add(Dense(256, activation='relu')) model.add(Dense(128, activation='relu')) model.add(Dense(1, activation="linear")) return model optimizer = tf.keras.optimizers.Adam() model.compile(optimizer=optimizer, loss='mean_absolute_error') history = model.fit(X_train, y_train, epochs=10, batch_size=1024, verbose=1)
I got this error :
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).
embeddings and genres are of type numpy.ndarray.
I've searched for a hint but with no result. I would appreciate if you could help me figure out where the error came from. (I also have tried to convert embeddings and genres to other types)
df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 100004 entries, 0 to 100003 Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 userId 100004 non-null int64 1 movieId 100004 non-null int64 2 rating 100004 non-null float64 3 embeddings 100004 non-null object 4 genres 100004 non-null object dtypes: float64(1), int64(2), object(2)
-
Good Tensor Flow Tutorials For Basic Image Recognition
Does anyone know of any easy to digest, reliable and up to date tutorials for how to do basic image recognition using Tensor Flow in Python? I am trying to find one for a younger student of mine who is having trouble comprehending traditional documentation and needs a good resource that they can work through at home, since I cant be there all the time to walk them through things step by step.
-
How to choose an input_shape in tensorflow?
I'm currently building a model to forcast weather using kaggle Istanbul datasets. I've downloaded and loaded the csv file. I select all the interesting columns, convert them, add code to categorical data and so on.
df = pd.read_csv('/content/Istanbul Weather Data.csv',sep=",") #store our dataFrame into Df df = df[["DateTime","Condition","Rain","MaxTemp","MinTemp","AvgWind","AvgHumidity","AvgPressure"]] df["DateTime"] = pd.to_datetime(df["DateTime"].str.replace(".","/")) #Now let's associate a code to our str data which are categorical : #Condition : sunny = 0, Partly cloudy = 1, overcast =2, cloudy=3, patchy rain possible=4 #Rain : nothing = 0, rain = 1, snow = 2( for another dataset) for ind in df.index: if df['Rain'][ind] != 0.0 : df['Rain'][ind] = 1.0 else: df['Rain'][ind] = 0.0 if df['Condition'][ind]== "Sunny": df['Condition'][ind] = 0.0 elif df['Condition'][ind] == "Partly cloudy" : df['Condition'][ind] = 1.0 elif df['Condition'][ind] == "Overcast" : df['Condition'][ind] = 2.0 elif df['Condition'][ind] == "Cloudy" : df['Condition'][ind] = 3.0 else : df['Condition'][ind] = 4.0 df['Rain'] = df['Rain'].astype(int) df['Condition'] = df['Condition'].astype(int) df.head()
Then I extract my features (and convert it into ndarray) and my labels :
df = df[["Condition","Rain","MaxTemp","MinTemp","AvgWind","AvgHumidity","AvgPressure"]] df_features = df.copy() df_labels = df_features.pop('Condition') df_features = np.array(df_features) df_features
Then I want to write my model :
model = tf.keras.Sequential([ tf.keras.layers.Dense(units=64, input_shape=(6,)), tf.keras.layers.Dense(units=5, activation='softmax') ]) model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.01), loss='categorical_crossentropy') model.fit(df_features,df_labels,epochs=30)
When I want to fit it I got this error :
ValueError: Shapes (None, 1) and (None, 5) are incompatible
I don't know how to choose the right input_shape or how to reshape my dataset...
If anyone has a hint I would glad to listen it !
And if you have some recommandations to improve or optimize my model that would be cool if you tell me it !
Thanks in advance !
Regards, Baptiste ZLOCH
-
Implementation of SGD with momentum slows net down
I've been working on a neural net class that I can later turn into a library of my own. Primarily doing this to get a good understanding of nets and I've been reading all the formulas from pure maths lectures so I might have a few small details wrong. (I had no idea how to before I started this)
In this net I have coded in a normal SGD algorithm and then an momentum algorithm (or atleast what I think it is). When I run the net on my simple data set using SGD, it works perfectly, no problems at all. But if I try using SGD with momentum, the net does not learn at all, even after 10000 iterations the loss stays around 0.7.
I have been back and forth, referencing the formula from many places and while I still doubt I completely understand, I feel like it is definitely something with my code but I cant figure it out. I have tried many combinations of alpha and lambda values, many reasonable combinations of layers and neurons(specifically more than one hidden layer with momentum formula, but it doesnt work with 1 layer either).I am going to post the code for the full net, so if anyone is willing to just scan through it quick and see if there's anything that seems obviously wrong, that would be much appreciated. I feel the fault might ly in updateweights() function since that is where most of the calculation happens but it could also be in the calcema() function.
I have tried changing the weight update formula from W = W - (alpha * partial derivative) to W = W + ( alpha * PD) (and keeping the PD positive instead of making it negative), also tried removing the regularizer for the momentum update formula but none of it has actually made a difference.
I am still very new to this, trying my best so any feedback is appreciated.
Here is a sample from the input file:in: 0.6 0.34 0.32 0.78 out: 1.0 0.0 0.0 in: 0.36 0.52 0.75 0.67 out: 1.0 0.0 0.0 in: 0.29 0.034 0.79 0.5 out: 0.0 1.0 0.0 in: 0.21 0.29 0.47 0.62 out: 0.0 1.0 0.0 in: 0.67 0.57 0.42 0.19 out: 0.0 1.0 0.0 in: 0.48 0.22 0.79 0.0096 out: 0.0 1.0 0.0 in: 0.75 0.48 0.61 0.67 out: 1.0 0.0 0.0 in: 0.41 0.96 0.65 0.074 out: 1.0 0.0 0.0 in: 0.19 0.88 0.68 0.1 out: 0.0 1.0 0.0 in: 0.9 0.89 0.95 0.45 out: 1.0 0.0 0.0 in: 0.71 0.58 0.95 0.013 out: 1.0 0.0 0.0 in: 0.66 0.043 0.073 0.98 out: 0.0 1.0 0.0 in: 0.12 0.37 0.2 0.22 out: 0.0 0.0 1.0 in: 0.11 0.38 0.54 0.64 out: 0.0 1.0 0.0 in: 0.42 0.81 0.94 0.98 out: 1.0 0.0 0.0
if anyone would like the full input file, let me know, I just dont know how to post files on here but I will find a way. So my problem specifically is that when I use SGD with momentum (or what I think is SGD with momentum), my net does not learn at all and gets stuck at a loss of 0.7... but if I use normal SGD it works perfectly.
The code:
#include <iostream> #include <vector> #include <iomanip> #include <cmath> #include <random> #include <fstream> #include <chrono> #include <sstream> #include <string> #include <assert.h> double Relu(double val) { if (val < 0) return 0.01 * (exp(val) - 1); else return val; } double Reluderiv(double val) { if (val < 0) return Relu(val) + 0.01; else return 1; } double randdist(double x, double y) { return sqrt(2.0 / (x + y)); } int randomt(int x, int y) { std::random_device rd; std::mt19937 mt(rd()); std::uniform_real_distribution<double> dist(x, y); return round(dist(mt)); } class INneuron { public: double val{}; std::vector <double> weights{}; std::vector <double> weightderivs{}; std::vector <double> emavals{}; }; class HIDneuron { public: double preactval{}; double actval{}; double actvalPD{}; double preactvalPD{}; std::vector <double> weights{}; std::vector <double> weightderivs{}; std::vector <double> emavals{}; double bias{}; double biasderiv{}; double biasema{}; }; class OUTneuron { public: double preactval{}; double actval{}; double preactvalPD{}; double bias{}; double biasderiv{}; double biasema{}; }; class Net { public: Net(int netdimensions, int hidlayers, int hidneurons, int outneurons, int inneurons, double lambda, double alpha) { NETDIMENSIONS = netdimensions; HIDLAYERS = hidlayers; HIDNEURONS = hidneurons; OUTNEURONS = outneurons; INNEURONS = inneurons; Lambda = lambda; Alpha = alpha; } void defineoptimizer(std::string optimizer); void Feedforward(const std::vector <double>& invec); void Backprop(const std::vector <double>& targets); void Updateweights(); void printvalues(double totalloss); void Initweights(); void softmax(); double regularize(double weight,std::string type); double lossfunc(const std::vector <double>& target); void calcema(int Layernum, int neuron, int weight, std::string layer, std::string BorW); private: INneuron Inn; HIDneuron Hidn; OUTneuron Outn; std::vector <std::vector <HIDneuron>> Hidlayers{}; std::vector <INneuron> Inlayer{}; std::vector <OUTneuron> Outlayer{}; double NETDIMENSIONS{}; double HIDLAYERS{}; double HIDNEURONS{}; double OUTNEURONS{}; double INNEURONS{}; double Lambda{}; double Alpha{}; double loss{}; int optimizerformula{}; }; void Net::defineoptimizer(std::string optimizer) { if (optimizer == "ExpAvrg") { optimizerformula = 1; } else if (optimizer == "SGD") { optimizerformula = 2; } else if (optimizer == "Adam") { optimizerformula = 3; } else if (optimizer == "MinibatchSGD") { optimizerformula = 4; } else { std::cout << "no optimizer matching description" << '\n'; abort(); } } double Net::regularize(double weight,std::string type) { if (type == "L1") { double absval{ weight }; /*if (weight < 0) absval = weight * -1; else if (weight > 0 || weight == 0) absval = weight; else;*/ if (absval > 0.0) return 1.0; else if (absval < 0.0) return -1.0; else if (absval == 0.0) return 0.0; else return 2; } else if (type == "l2") { double absval{}; if (weight < 0.0) absval = weight * -1.0; else absval = weight; return (2.0 * absval); } else { std::cout << "no regularizer recognized" << '\n'; abort(); } } void Net::softmax() { double sum{}; for (size_t Osize = 0; Osize < Outlayer.size(); Osize++) { sum += exp(Outlayer[Osize].preactval); } for (size_t Osize = 0; Osize < Outlayer.size(); Osize++) { Outlayer[Osize].actval = exp(Outlayer[Osize].preactval) / sum; } } void Net::Initweights() { unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); std::default_random_engine generator(seed); std::normal_distribution<double> distribution(0.0, 1.0); for (int WD = 0; WD < HIDLAYERS + 1; WD++) { if (WD == 0) { for (int WL = 0; WL < INNEURONS; WL++) { Inlayer.push_back(Inn); for (int WK = 0; WK < HIDNEURONS; WK++) { double val = distribution(generator) * randdist(INNEURONS, HIDNEURONS); Inlayer.back().weights.push_back(val); Inlayer.back().weightderivs.push_back(0.0); Inlayer.back().emavals.push_back(0.0); } } } else if (WD < HIDLAYERS && WD != 0) { Hidlayers.push_back(std::vector <HIDneuron>()); for (int WL = 0; WL < HIDNEURONS; WL++) { Hidlayers.back().push_back(Hidn); for (int WK = 0; WK < HIDNEURONS; WK++) { double val = distribution(generator) * randdist(HIDNEURONS, HIDNEURONS); Hidlayers.back().back().weights.push_back(val); Hidlayers.back().back().weightderivs.push_back(0.0); Hidlayers.back().back().emavals.push_back(0.0); } Hidlayers.back().back().bias = 0.0; Hidlayers.back().back().biasderiv = 0.0; Hidlayers.back().back().biasema = 0.0; } } else if (WD == HIDLAYERS) { Hidlayers.push_back(std::vector <HIDneuron>()); for (int WL = 0; WL < HIDNEURONS; WL++) { Hidlayers.back().push_back(Hidn); for (int WK = 0; WK < OUTNEURONS; WK++) { double val = distribution(generator) * randdist(HIDNEURONS, OUTNEURONS); Hidlayers.back().back().weights.push_back(val); Hidlayers.back().back().weightderivs.push_back(0.0); Hidlayers.back().back().emavals.push_back(0.0); } Hidlayers.back().back().bias = 0.0; Hidlayers.back().back().biasderiv = 0.0; Hidlayers.back().back().biasema = 0.0; } } } for (int i = 0; i < OUTNEURONS; i++) { Outlayer.push_back(Outn); Outlayer.back().bias = 0.0; Outlayer.back().biasderiv = 0.0; Outlayer.back().biasema = 0.0; } } void Net::Feedforward(const std::vector <double>& invec) { for (size_t I = 0; I < Inlayer.size(); I++) { Inlayer[I].val = invec[I]; } for (size_t h = 0; h < Hidlayers[0].size(); h++) { double preval = Hidlayers[0][h].bias; for (size_t I = 0;I < Inlayer.size(); I++) { preval += Inlayer[I].val * Inlayer[I].weights[h]; } Hidlayers[0][h].preactval = preval; Hidlayers[0][h].actval = Relu(preval); } for (size_t H = 1; H < Hidlayers.size();H++) { size_t prevh = H - 1; for (size_t h = 0; h < Hidlayers[H].size(); h++) { double preval = Hidlayers[H][h].bias; for (size_t p = 0; p < Hidlayers[prevh].size(); p++) { preval += Hidlayers[prevh][p].actval * Hidlayers[prevh][p].weights[h]; } Hidlayers[H][h].preactval = preval; Hidlayers[H][h].actval = Relu(preval); } } for (size_t O = 0; O < Outlayer.size(); O++) { size_t lhid = Hidlayers.size() - 1; double preval = Outlayer[O].bias; for (size_t h = 0; h < Hidlayers[lhid].size(); h++) { preval += Hidlayers[lhid][h].actval * Hidlayers[lhid][h].weights[O]; } Outlayer[O].preactval = preval; } } void Net::Backprop(const std::vector <double>& targets) { for (size_t O = 0; O < Outlayer.size(); O++) { double PDval{}; PDval = targets[O] - Outlayer[O].actval; PDval = PDval * -1.0; Outlayer[O].preactvalPD = PDval; } for (size_t H = Hidlayers.size(); H > 0; H--) { size_t Top = H; size_t Current = H - 1; for (size_t h = 0; h < Hidlayers[Current].size(); h++) { double actPD{}; double PreactPD{}; double biasPD{}; for (size_t hw = 0; hw < Hidlayers[Current][h].weights.size(); hw++) { double PDval{}; if (H == Hidlayers.size()) { PDval = Outlayer[hw].preactvalPD * Hidlayers[Current][h].actval; biasPD = Outlayer[hw].preactvalPD; Outlayer[hw].biasderiv = biasPD; actPD += Hidlayers[Current][h].weights[hw] * Outlayer[hw].preactvalPD; calcema(0, hw, 0, "Outlayer", "Bias"); } else { PDval = Hidlayers[Top][h].preactvalPD * Hidlayers[Current][h].actval; actPD += Hidlayers[Current][h].weights[hw] * Hidlayers[Top][h].preactvalPD; } Hidlayers[Current][h].weightderivs[hw] = PDval; calcema(Current, h, hw, "Hidlayer", "Weight"); } if (H != Hidlayers.size()) { biasPD = Hidlayers[Top][h].preactvalPD; Hidlayers[Top][h].biasderiv = biasPD; calcema(Top, h, 0, "Hidlayer", "Bias"); } Hidlayers[Current][h].actvalPD = actPD; PreactPD = Hidlayers[Current][h].actvalPD * Reluderiv(Hidlayers[Current][h].preactval); Hidlayers[Current][h].preactvalPD = PreactPD; actPD = 0; } } for (size_t I = 0; I < Inlayer.size(); I++) { double PDval{}; for (size_t hw = 0; hw < Inlayer[I].weights.size(); hw++) { PDval = Hidlayers[0][hw].preactvalPD * Inlayer[I].val; Inlayer[I].weightderivs[hw] = PDval; double biasPD = Hidlayers[0][hw].preactvalPD; Hidlayers[0][hw].biasderiv = biasPD; } } } //PROBABLE CULPRIT void Net::Updateweights() { for (size_t I = 0; I < Inlayer.size(); I++) { double PD{}; for (size_t iw = 0; iw < Inlayer[I].weights.size(); iw++) { if (optimizerformula == 2) { PD = (Inlayer[I].weightderivs[iw] * -1.0) - (Lambda * regularize(Inlayer[I].weights[iw], "L1")); Inlayer[I].weights[iw] = Inlayer[I].weights[iw] + (Alpha * PD); } else if (optimizerformula == 1) { PD = (Inlayer[I].emavals[iw] * -1.0) - (Lambda * regularize(Inlayer[I].weights[iw], "L1")); Inlayer[I].weights[iw] = Inlayer[I].weights[iw] + (Alpha * PD); } } } for (size_t H = 0; H < Hidlayers.size(); H++) { for (size_t h = 0; h < Hidlayers[H].size(); h++) { double PD{}; for (size_t hw = 0; hw < Hidlayers[H][h].weights.size(); hw++) { if (optimizerformula == 2) { PD = (Hidlayers[H][h].weightderivs[hw] * -1.0) - (Lambda * regularize(Hidlayers[H][h].weights[hw], "L1")); Hidlayers[H][h].weights[hw] = Hidlayers[H][h].weights[hw] + (Alpha * PD); } else if (optimizerformula == 1) { PD = (Hidlayers[H][h].emavals[hw] * -1.0) - (Lambda * regularize(Hidlayers[H][h].weights[hw], "L1")); Hidlayers[H][h].weights[hw] = Hidlayers[H][h].weights[hw] + (Alpha * PD); } } if (optimizerformula == 1) { PD = Hidlayers[H][h].biasema * -1.0; Hidlayers[H][h].bias = Hidlayers[H][h].bias + (Alpha * PD); } else if (optimizerformula == 2) { PD = Hidlayers[H][h].biasderiv * -1.0; Hidlayers[H][h].bias = Hidlayers[H][h].bias + (Alpha * PD); } } } for (size_t biases = 0; biases < Outlayer.size(); biases++) { if (optimizerformula == 2) { double PD = Outlayer[biases].biasderiv * -1.0; Outlayer[biases].bias = Outlayer[biases].bias + (Alpha * PD); } else if (optimizerformula == 1) { double PD = Outlayer[biases].biasema * -1.0; Outlayer[biases].bias = Outlayer[biases].bias + (Alpha * PD); } } } void Net::printvalues(double totalloss) { for (size_t Res = 0; Res < Outlayer.size(); Res++) { std::cout << Outlayer[Res].actval << " / "; } std::cout << '\n' << "loss = " << totalloss << '\n'; } double Net::lossfunc(const std::vector <double>& target) { int pos{ -1 }; double val{}; for (size_t t = 0; t < target.size(); t++) { pos += 1; if (target[t] > 0) { break; } } val = -log(Outlayer[pos].actval); return val; } //OTHER PROBABLE CULPRIT void Net::calcema(int Layernum, int neuron, int weight, std::string layer, std::string BorW ) { static double Beta{ 0.9 }; if (BorW == "Weight") { if (layer == "Inlayer") { Inlayer[neuron].emavals[weight] = (Beta * Inlayer[neuron].emavals[weight]) + ((1.0 - Beta) * Inlayer[neuron].weightderivs[weight]); } else if (layer == "Hidlayers") { Hidlayers[Layernum][neuron].emavals[weight] = (Beta * Hidlayers[Layernum][neuron].emavals[weight]) + ((1.0 - Beta) * Hidlayers[Layernum][neuron].weightderivs[weight]); } } else if (BorW == "Bias") { if (layer == "Hidlayers") { Hidlayers[Layernum][neuron].biasema = (Beta * Hidlayers[Layernum][neuron].biasema) + ((1.0 - Beta) * Hidlayers[Layernum][neuron].biasderiv); } else if (layer == "Outlayer") { Outlayer[neuron].biasema = (Beta * Outlayer[neuron].biasema) + ((1.0 - Beta) * Outlayer[neuron].biasderiv); } } } int main() { std::vector <double> innums{}; std::vector <double> outnums{}; std::vector <std::string> INstrings{}; std::vector <std::string> OUTstrings{}; std::string nums{}; std::string in{}; std::string out{}; double totalloss{}; double loss{}; double single{}; int batchcount{0}; Net net(0, 2, 4, 3, 4, 0.0001, 0.006); net.Initweights(); net.defineoptimizer("ExpAvrg"); std::ifstream file("N.txt"); while (file.is_open()) { int count{ 0 }; while (file >> nums) { if (nums == "in:") { count += 1; std::getline(file, in); INstrings.push_back(in); } else if (nums == "out:") { count += 1; std::getline(file, out); OUTstrings.push_back(out); } else; } break; } for (int epoch = 0; epoch < 50000; epoch++) { int random = randomt(0, 99); std::string invals = INstrings[random]; std::string outvals = OUTstrings[random]; std::stringstream in(invals); std::stringstream out(outvals); std::cout << "fetching" << '\n'; while (in >> single) { innums.push_back(single); } while (out >> single) { outnums.push_back(single); } std::cout << "epoch " << epoch << '\n'; std::cout << "In nums: " << '\n'; for (auto element : innums) std::cout << element << " / "; std::cout << '\n' << "targets: " << '\n'; for (auto element : outnums) std::cout << element << " / "; std::cout << '\n'; batchcount += 1; net.Feedforward(innums); net.softmax(); loss += net.lossfunc(outnums); totalloss = loss / batchcount; net.printvalues(totalloss); net.Backprop(outnums); net.Updateweights(); innums.clear(); outnums.clear(); } std::cout << "in size: "<< INstrings.size() << '\n'; std::cout << "out size: " << OUTstrings.size() << '\n'; }
-
Core ML. Style transfer, maximum size of input/output
I set input and output image sizes equal to
2048x2048
. And have the following error in my iPhone 7:exception=Espresso exception: "Context exceeded": Cannot create 2147483648-byte metal heap. status=-13
But it works fine in simulator. Is it possible to make a prediction to be sure that it will be enough memory to apply style transfer? And maybe there is some list with recommendations about input/output model size?
-
MLModel style transfer prediction. Scale effect(brushstrokes)
I have style transfer model which is trained by
pytorch
and converted by onnx tomlmodel
. Thestyle-image
was1500x2000
. By usingcoremltools
I set two sizes:256x256
and1500x2000
. Now I can pass two image sizes to prediction process. Here are results:On the left side it is
1500x2000
image, and on the right side is256x256
(scaled up after processing) Is it possible to pass big image but have bigger size of brushstrokes as you can see on image on the right? So I want to keep image size and quality (1500x2000
) but change the size of style(brushstrokes). Or it is not possible and it is totally depend ofimage-style
size I was using to train model.