keras-loss-functions/huber-loss.py at master - GitHub
文章推薦指數: 80 %
Keras model demonstrating Huber loss. ''' from keras.datasets import boston_housing. from keras.models import Sequential. from keras.layers import Dense. Skiptocontent {{message}} christianversloot / keras-loss-functions Public Notifications Fork 2 Star 4 Code Issues 0 Pullrequests 0 Actions Projects 0 Security Insights More Code Issues Pullrequests Actions Projects Security Insights Permalink master Branches Tags Couldnotloadbranches Nothingtoshow {{refName}} default Couldnotloadtags Nothingtoshow {{refName}} default Atagalreadyexistswiththeprovidedbranchname.ManyGitcommandsacceptbothtagandbranchnames,socreatingthisbranchmaycauseunexpectedbehavior.Areyousureyouwanttocreatethisbranch? keras-loss-functions/huber-loss.py / Jumpto huber_loss_wrapper Function huber_loss_wrapped_function Function Gotofile Gotofile T Gotoline L Gotodefinition R Copypath Copypermalink Thiscommitdoesnotbelongtoanybranchonthisrepository,andmaybelongtoaforkoutsideoftherepository. Cannotretrievecontributorsatthistime 54lines(46sloc) 1.98KB Raw Blame Editthisfile E OpeninGitHubDesktop OpenwithDesktop Viewraw Viewblame ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters. LearnmoreaboutbidirectionalUnicodecharacters Showhiddencharacters ''' KerasmodeldemonstratingHuberloss ''' fromkeras.datasetsimportboston_housing fromkeras.modelsimportSequential fromkeras.layersimportDense fromkeras.lossesimporthuber_loss importnumpyasnp importmatplotlib.pyplotasplt #DefinetheHuberlosssothatitcanbeusedwithKeras defhuber_loss_wrapper(**huber_loss_kwargs): defhuber_loss_wrapped_function(y_true,y_pred): returnhuber_loss(y_true,y_pred,**huber_loss_kwargs) returnhuber_loss_wrapped_function #Loaddata (x_train,y_train),(x_test,y_test)=boston_housing.load_data() #Settheinputshape shape_dimension=len(x_train[0]) input_shape=(shape_dimension,) print(f'Featureshape:{input_shape}') #Createthemodel model=Sequential() model.add(Dense(16,input_shape=input_shape,activation='relu',kernel_initializer='he_uniform')) model.add(Dense(8,activation='relu',kernel_initializer='he_uniform')) model.add(Dense(1,activation='linear')) #Configurethemodelandstarttraining model.compile(loss=huber_loss_wrapper(delta=1.5),optimizer='adam',metrics=['mean_absolute_error']) history=model.fit(x_train,y_train,epochs=250,batch_size=1,verbose=1,validation_split=0.2) #Testthemodelaftertraining test_results=model.evaluate(x_test,y_test,verbose=1) print(f'Testresults-Loss:{test_results[0]}-MAE:{test_results[1]}') #Plothistory:HuberlossandMAE plt.plot(history.history['loss'],label='Huberloss(trainingdata)') plt.plot(history.history['val_loss'],label='Huberloss(validationdata)') plt.title('BostonHousingPriceDatasetregressionmodel-Huberloss') plt.ylabel('Lossvalue') plt.xlabel('No.epoch') plt.legend(loc="upperleft") plt.show() plt.title('BostonHousingPriceDatasetregressionmodel-MAE') plt.plot(history.history['mean_absolute_error'],label='MAE(trainingdata)') plt.plot(history.history['val_mean_absolute_error'],label='MAE(validationdata)') plt.ylabel('Lossvalue') plt.xlabel('No.epoch') plt.legend(loc="upperleft") plt.show() Copylines Copypermalink Viewgitblame Referenceinnewissue Go Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.
延伸文章資訊
- 1using tensorflow huber loss in keras - splunktool
A loss function is one of the two arguments required for compiling a Keras model:. All losses are...
- 2Using Tensorflow Huber loss in Keras - Stack Overflow
I am trying to use huber loss in a keras model (writing DQN), but I am getting bad result, I thin...
- 3Losses - Keras
Losses. The purpose of loss functions is to compute the quantity that a model ... cosine_similari...
- 4tf.keras.losses.Huber | TensorFlow v2.10.0
tf.keras.losses.Huber ; delta, A float, the point where the Huber loss function changes from a qu...
- 5machine-learning-articles/using-huber-loss-in-keras.md - GitHub
Summary and code example: Huber Loss with TensorFlow 2 and Keras ... Loss functions are used to c...