TensorFlow的培训实用程序。

train的Python项目详细描述


TensorFlow的培训实用程序。

安装

Install TensorFlow

pip install tensorflow

然后运行:

pip install train

建议使用virtual environment

开始

fromtrainimportModel,GradientDescentimporttensorflowastf# Define the network architecture - layers, number of units, activations etc.defnetwork(inputs):hidden=tf.layers.Dense(units=64,activation=tf.nn.relu)(inputs)outputs=tf.layers.Dense(units=10)(hidden)returnoutputs# Configure the learning process - loss, optimizer, evaluation metrics etc.model=Model(network,loss='sparse_softmax_cross_entropy',optimizer=GradientDescent(0.001),metrics=['accuracy'])# Train the model using training datamodel.train(x_train,y_train,epochs=30,batch_size=128)# Evaluate the model performance on test or validation dataloss_and_metrics=model.evaluate(x_test,y_test)# Use the model to make predictions for new datapredictions=model.predict(x)# or call the model directlypredictions=model(x)

更多配置选项可用:

model=Model(network,loss='sparse_softmax_cross_entropy',optimizer=GradientDescent(0.001),metrics=['accuracy'],model_dir='/tmp/my_model')

您还可以使用自定义的损失和度量函数:

defcustom_loss(labels,outputs):passdefcustom_metric(labels,outputs):passmodel=Model(network,loss=custom_loss,optimizer=GradientDescent(0.001),metrics=['accuracy',custom_metric])

模型函数

要获得更多控制,可以使用Estimator类在函数内部配置模型:

fromtrainimportEstimator,PREDICTimporttensorflowastfdefmodel(features,labels,mode):# Define the network architecturehidden=tf.layers.Dense(units=64,activation=tf.nn.relu)(features)outputs=tf.layers.Dense(units=10)(hidden)predictions=tf.argmax(outputs,axis=1)# In prediction mode, simply return predictions without configuring learning processifmode==PREDICT:returnpredictions# Configure the learning process for training and evaluation modesloss=tf.losses.sparse_softmax_cross_entropy(labels,outputs)optimizer=tf.train.GradientDescentOptimizer(0.001)accuracy=tf.metrics.accuracy(labels,predictions)returndict(loss=loss,optimizer=optimizer,metrics={'accuracy':accuracy})# Create the model using model functionmodel=Estimator(model)# Train the modelmodel.train(x_train,y_train,epochs=30,batch_size=128)

mode参数指定模型是用于训练、评估还是预测

培训模式

对于像Dropout这样的层,可以使用training mode变量:

fromtrainimporttrainingx=tf.layers.Dropout(rate=0.4)(x,training=training())

ModelEstimator类自动管理训练模式变量。要手动更改,请使用:

# Enter training modetraining(True,session=session)# Exit training modetraining(False,session=session)

许可证

MIT

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
带truezip的java拆分zip   java Spring,AppEngine:在AppEngine的数据源中添加postgresql url   java Android coverflow   java以编程方式创建复合过滤器,以在log4j 2中定义多个过滤器   java jpa eclipselink异常[eclipselink 4002]   中的java WordNet数据库目录相对路径。罐子   java无法在Spring Boot 2/3中显示登录的用户   java Onetomany:未找到联接表错误   java数据模型演化   java方法在类型列表中添加的(对象)不适用于参数(int)意味着什么?   用java打印两个数组   java SNMP4J发送从不超时   java添加/删除联系人(EditText)+类别(SpinnerBox),可以根据需要动态添加/删除多个联系人   语句和PreparedStatement之间的java差异   java在运行作为JAR归档文件分发的项目时加载图像等资源   来自应用程序或外部服务器的java Cron作业   多线程Java并发:并发添加和清除列表项   java更改单元测试的私有方法行为