|
| 1 | +<a href="/v4/Sentiment-Analysis/Vectorizing-Text.md">< Previous</a> |
| 2 | + |
| 3 | +<a href="/v4/Sentiment-Analysis/Making-a-Network.md">Next ></a> |
| 4 | +<hr> |
| 5 | +<h1>Creating the Network</h1> |
| 6 | +So far you have cleaned up and prepared the data and vectorized it. Now, it's officially time to create the network to understand the text. Now that we have text as number input and three categories of output, this is essentially just a classification problem. Thus, you can create a network that does classification. |
| 7 | +<br> |
| 8 | +In order to create the model, you will use a sequential model. You will create an input layer, several calculation layers, dropout layers, and an output layer. |
| 9 | +<br> |
| 10 | +Follow these steps to create the model. |
| 11 | +<br> |
| 12 | +1. Continue working in your Preparing Data and Vectorizing Text notebook. |
| 13 | +<br> |
| 14 | +2. Create a sequential model |
| 15 | +<pre>model = Sequential()</pre> |
| 16 | +<img src="https://i.imgur.com/SFUCa6U.png"> |
| 17 | +<h2>Input Layer</h2> |
| 18 | +The input layer is the first layer of the network. It takes all of the text input and starts to perform calculations with it. This layer needs to be pretty large so that the network can start to get an idea of what it's dealing with. This one is set to 4000 neurons, but if you want to go larger you can! |
| 19 | +<br> |
| 20 | +Also, for this layer, you will be deciding on an activation algorithm. Relu is one that works well for these purposes. |
| 21 | +<br> |
| 22 | +3. Add an input layer to the network. |
| 23 | +<pre>model.add(Dense(units=4000,activation="relu"))</pre> |
| 24 | +<img src="https://i.imgur.com/y1cZlmX.png"> |
| 25 | +<h2>Dropout Layers</h2> |
| 26 | +Another important aspect of this model is the dropout layers. These help prevent the network from relying too much on specific neurons and force all the neurons to do some work. This is especially important for large networks like this one. This one is set to drop out at a rate of 0.5 which means half of the neurons. |
| 27 | +<br> |
| 28 | +4. Add a dropout layer. |
| 29 | +<pre>model.add(Dropout(0.5))</pre> |
| 30 | +<img src="https://i.imgur.com/ZSkZTqD.png"> |
| 31 | +<h2>Calculation Layers</h2> |
| 32 | +Once you have the input, it's time to start actually doing calculations with it. This part of the network will do the bulk of the calculation. It's important to include dropout layers in this part of the network as well so that the network continues to be versatile. |
| 33 | +<br> |
| 34 | +5. Add some calculation layers to this network. You can add as many as you like. |
| 35 | +<pre> |
| 36 | +model.add(Dense(units=2000,activation="relu")) |
| 37 | +model.add(Dropout(0.5)) |
| 38 | +model.add(Dense(units=500,activation="relu")) |
| 39 | +model.add(Dropout(0.5)) |
| 40 | +model.add(Dense(units=250,activation="relu")) |
| 41 | +model.add(Dropout(0.5)) |
| 42 | +</pre> |
| 43 | +<img src="https://i.imgur.com/5MWChwo.png"> |
| 44 | +<h2>Output Layer</h2> |
| 45 | +Finally, you will add a layer that outputs the network's decision. The number of neurons for this layer is going to be the number of possible categories. In this case, there are three possible outputs: positive, negative, neutral. The activation algorithm will be softmax, an algorithm that creates a probability that each review belongs in each category. |
| 46 | +<br> |
| 47 | +6. Add an output layer. This needs to have three neurons: one for each possible outcome. |
| 48 | +<pre>model.add(Dense(units=3,activation="softmax"))</pre> |
| 49 | +<img src="https://i.imgur.com/TemDCn7.png"> |
| 50 | +<h2>Compiling the Network</h2> |
| 51 | +Now that you have all the layers set up, it's time to finally compile the network. First, you will create a variable to hold the optimizer and then you will compile the network. |
| 52 | +<br> |
| 53 | +The optimizer is going to be a general-purpose algorithm called adam that will work well for this purpose. The loss algorithm is categorical crossentropy since this problem is bout sorting data into categories, and the metric to watch is accuracy. |
| 54 | +<br> |
| 55 | +7. Create a variable to keep track of the optimizer. For this, you are going to use Adam, an extremely effective multi-purpose algorithm. |
| 56 | +<pre>opt = tf.keras.optimizers.Adam(learning_rate=0.001)</pre> |
| 57 | +8. Compile the model. |
| 58 | +<pre>model.compile(loss="categorical_crossentropy",optimizer=opt,metrics=["accuracy"])</pre> |
| 59 | +<img src="https://i.imgur.com/7BV5bDk.png"> |
| 60 | +<h2>Fit Data to the Network</h2> |
| 61 | +Next, you will train the network using the data that you have prepared. During this stage, you will also decide on batch size and the number of epochs. Finally, you will set the validation data to the test datasets you created. |
| 62 | +<br> |
| 63 | +9. Fit the data to the model. |
| 64 | +<pre>model.fit(x=x_rev_train_v,y=y_pol_train,batch_size=256,epochs=10,validation_data=(x_rev_test_v,y_pol_test))</pre> |
| 65 | +<img src="https://i.imgur.com/B3K6wxh.png"> |
| 66 | +<h2>Evaluating the Network</h2> |
| 67 | +Once the network is done training, you will get the scores from the model, and print out the accuracy. This will give you a good idea of how the network performs on the test data. This network should give an accuracy of about 0.70, which is pretty good. |
| 68 | +<br> |
| 69 | +10. Get the scores by evaluating the model. |
| 70 | +<pre>scores = model.evaluate(x_rev_test_v,y_pol_test,verbose=0)</pre> |
| 71 | +11. Print the accuracy. |
| 72 | +<pre>print('Test accuracy: ',scores[1],'%')</pre> |
| 73 | +<img src="https://i.imgur.com/rPbgJmz.png"> |
| 74 | +<h2>Save the Model</h2> |
| 75 | +In the same cell as all the other model creation code, make sure you add the code to export the model. This way you won't have to wait for it to train in the future. |
| 76 | +<br> |
| 77 | +12. Export the model when it's done for future use. |
| 78 | +<pre>model.save('sentiments.h5')</pre> |
| 79 | +13. Run the cell to train the model. |
| 80 | +<br> |
| 81 | +<img src="https://i.imgur.com/oxXY0Uh.png"> |
| 82 | +<br> |
| 83 | +This network is very large and contains a lot of information. It will take quite a while to train, so go ahead and let it run as long as it needs. Just be sure to leave the tab open while it makes its calculations. |
| 84 | +<br> |
| 85 | +Here's what your project could look like at this point: |
| 86 | +<br> |
| 87 | +<img src="https://i.imgur.com/W3PlfJw.png"> |
| 88 | +<h1>Recap</h1> |
| 89 | +Creating a network that can classify text based on sentiment required a sequential network with a bunch of layers of neurons. You created, trained, and exported this model. |
0 commit comments