Mental Health, Project, Python, Wellness Journey

Neural Network Cognitive Behavioral Therapy Project 3: Inner Demon, Outer Angel, & Inner Angel

**Please review the trigger warnings and disclaimers for this project or continue at your own risk. **

The first few posts on this project has outlined my personal struggle with mental illness that has led me down this path. It is by no means a requisite to read, and has largely been a therapeutic way to rid myself of carrying around the memories of the last few years.

So I wondered, what if I use my letters to train a neural network called “My Inner Demon”? Perhaps then I can see what it says, how it thinks, what messages are the strongest in my depression.

In the following blog, I will perform the following experiment:

  1. Train a neural network based on my depression journals
  2. Take key statements and turn them into easy to read memes and post to an Instagram account called “My Inner Demon”
  3. Create a meme site called “My Outer Angel” where I post positive affirmations
  4. Feed these positive statements into the “Inner Demon” neural network and retrain it once a week and post it to an Instagram account called “My Inner Angel”
  5. Perform weekly analysis on the outputs to see how long it takes for positive affirmations to become the more prevalent output of the neural network

I used my original network that I trained my David Rose bot on, and fed in my depression notes. I also took the time to read the number of epochs to train in from a text file holding that variable that gets entered in my “save_and_train” program I describe next.

#!/usr/bin/env python3.6
from textgenrnn import textgenrnn
f = open('/home/etc/epochs.txt')
numepochs = int(f.readline())
t = textgenrnn('textgenrnn_weights.hdf5')
t.train_from_file('/home/etc/input.txt', num_epochs=numepochs)

I then made a script to run and save the outputs into a folder for further review. I learned about using variables to make directories and files, copy and rename files, and how to do a loop in bash.

#!/usr/bin/env python3.6
read -p "Enter number of epochs: " numepochs
echo $numepochs > /home/etc/epochs.txt

#train network
'/home/etc/train.py' 

#Make directory to store results from 0.1 to 1.0
mkdir -p /home/etc/epoch$numepochs

#loop through and run each temperature and save output in new directory
for i in 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
do
'/home/etc/Output Scripts/output_'$i'.py'  &> out.txt
cp /home/etc/out.txt /home/etc/epoch$numepochs/output$i.txt
done

To run this script, I simply need to type “source filepath/train_and_save.py”, so it will execute as if I individually wrote these commands in the terminal.

The output has a lot of junk in it, and preferably I’d like to have this in one big text file, so I feel there is more work to be done on the “train and save” script before I even begin analyzing the actual output.

#chop out excess info from script
linedel=$(grep -nr "100%" '/home/etc/epoch'$numepochs'/output_'$i'.txt' | gawk '{print $1}' FS=":") #find last line of garbage text
sed -e '1,'$linedel'd' '/home/etc/epoch'$numepochs'/output_'$i'.txt' >> '/home/etc/epoch'$numepochs'/output.txt' #remove lines & save output

Finally I got to the point in Bash programming that I need to rely on using variables for filepaths.

#!/usr/bin/env python3.6
#filepath variables
opath="/FilePathTo/Neural Network/Output Scripts"
fpath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

#ask user to input number of epochs
read -p "Enter number of epochs: " numepochs
echo $numepochs > "$fpath/epochs.txt"

#train network
"$fpath/train.py"

#Make directory to store results from 0.1 to 1.0
mkdir -p "$fpath/epoch$numepochs"

#loop through and run each temperature and save output in new directory
echo "" > "$fpath/epoch$numepochs/output.txt" #delete output content
for i in 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
do
#run output scripts
"$opath/output_$i.py"  &> out.txt 
#copy and save output
cp /home/catjitsu/out.txt "$fpath/epoch$numepochs/output_$i.txt"
#chop out excess info from script
linedel=$(grep -nr "100%" "$fpath/epoch$numepochs/output_$i.txt" | gawk '{print $1}' FS=":") #find last line of garbage text
sed -e '1,'$linedel'd' "$fpath/epoch$numepochs/output_$i.txt" >> "$fpath/epoch$numepochs/output.txt" #remove lines & save output
done

While the above script is great for running the network one at at time, I made another version that implemented a loop to automatically train and save for epochs 5 – 25 with 5 step intervals.

#set number of epochs
for numepochs in {5..25..5}
do
#REST OF CODE HERE
done

Similarly, I rewrote the train.py script to use variables to automatically use the the ‘input’ and ‘epoch’ files from its own directory.

#!/usr/bin/env python3.6
import sys
fpath=sys.path[0]
from textgenrnn import textgenrnn
f = open(fpath + '/epochs.txt')
numepochs = int(f.readline())
t = textgenrnn('textgenrnn_weights.hdf5')
t.train_from_file(fpath + '/input.txt', num_epochs=numepochs)

Lastly, I want to move the main “output” file to my externnal harddrive so it’ll be easy to use on my raspberry pi server. This required installing sshpass and setting up the connection. I also added in some fancy naming for the file, including taking the basename of the filepath to know what network created the output.

sudo apt-get install sshpass
ssh user@IP # press enter then type 'yes' when prompted

#Added the following to the end of my save_and_train scripts
bname=$(basename "$fpath") #get base name of directory
bname=${bname::-15} #remove NeuralNetwork from end
rpath="/etc/NNOutput" #location on external harddrive
sshpass -p 'PASSWORD' scp "$fpath/epoch$numepochs/output.txt" user@IP:$rpath/${bname// /_}$numepochs.txt

So now that I’ve got these scripts perfected, all I need to do is drop them into their respective folders and ensure it has the correct input text files.

I decided to start by training my Inner Demon neural network 20 epochs to see what I got, and I have to say, this is very revealing…

My next step in this project is to review this output with my therapist fiance (theon-say?) and figure out some “positive cognitions” that can combat all this negative garbage that runs through my head. I also am going to get a list of “positive affirmations” to say out loud “n” times a day. The mixture of cognitions and affirmations will then be fed in to train my “Outer Angel” network, along with my “Inner Angel” network.

After some discussions with Rachel, my therapist, and tinkering with my code the next several weeks I figured out my system. I am using words of affirmation in my Project_Outer_Angel Instagram bot, and then feeding them into the inner demon neural network.

To set a baseline, I started with the original inner demon neural network and trained it 25 epochs. I then generated 1000 output for only temperature 0.1 so that I don’t end up with too many crazy lines to aggregate.

I used a good old fashioned pivot table to count the number of times my network said a phrase and sorted in descending order. It’s not my prettiest table and it’s not the most in-depth analysis, but I’m doing this at home on a weekend so who am I trying to impress?

I could spend more time analyzing the data and words used, but honestly I’m just trying to quickly collect the data for now and run some preliminary analysis to determine if this Cognitive Behavioral Therapy with neural networks theory is even worth further study.


So now it’s time to add in my first round of inner angel neural network words of affirmation and retrain the network! Because it is recommended that affirmations are said 3 times a day, I’ll be entering each affirmation into the network 3 times.

After making a pivot table from the results, 3 of the 6 words of affirmations appeared on the top twenty phrases. They are still repeated far fewer times than anything else the network says, but it’s still surprising to see they were mentioned at all.

Since I’m a two weeks into this experiment, I was able to enter the next weeks worth of words of affirmation and retrain the network a second time.

Honestly, I was surprised at the results. 3 of the words of affirmation have now appeared in the top 10 results, and their repetitions have skyrocketed.

This definitely shows a LOT of promise of what is to come, but retraining the artificial depressed network is only half of my experiment. My goal is to retrain my own brain as well, so I have been checking this account in the real world and repeating the affirmations to myself daily. And honestly, I haven’t felt this mentally healthy in years.

And it’s all thanks to Dan Levy for inspiring me to make a neural network Instagram meme bot out of his character David Rose

Obviously, a lot of people smarter than me have told me that cognitive behavioral therapy works, but I was skeptical that it could work on someone as far gone as I viewed myself. But today I feel happier, healthier, and more stable than ever, and I truly believe that today is a good day!

Now that I am beginning to feel I have a sustainable way to improve my mental health, I have started to focus on improving my physical health. I made a personalized calorie tracking meal plan, and am even in the midst of building my first iPhone app to make sure that is a sustainable healthy change, too.

I LOVE the freedom that technology can provide people by offsetting our own weaknesses. I might be depressed, or have anxiety, or have ADD, but these bots and apps can help keep me focused and assist me in becoming a more mindful and healthy individual.

That will all be explained in my next project, but for now, I will be retraining the network every week to see how long it’ll take for Inner Demon to be completely eradicated with only Inner Angel remaining!


And that, ladies and gentleman, is as far as I got with documenting this experiment. It is now a year and a half later, and I did end up using the output to better tailor my therapy sessions. I continued to improve mentally up until a point, but did end up going to a treatment facility to help me with my issues. For the most part, I ended up there because of my job. I had a terrible boss at the time who was a large negative force. In fact, I couldn’t even sit in the same room with him without having severe anxiety followed by a depressive episode.

I was also diagnosed as having bi-polar II, and all the medications I was on were actually making me more mentally unstable. Now that I have the correct medication, and am no longer working for that horrible boss, I’ve been stable and free of depression for months.

I really wanted to finish this blog out because it’s been a year and a half, and I feel like it’s some sort of thing hanging over my head that I’ve been meaning to share. I personally feel that repeating the positive affirmations helped me in a time when I really needed it, and I definitely used my “Inner Demon” account as fodder for my therapy sessions. Overall, it was a worthwhile endeavor, and I wish that back then I would have documented my final results.

I will say, that eventually the positive affirmations continued the trend to dominate the negative ones. Which to me proved that you are what you think (robots included). With that, I’ll end it here, but in case anyone actually reads this, feel free to reach out if you have your own ideas for how technology can be used to shed light on mental illness.


Please Note:

I am not a professionally trained expert on the technology utilized in this project (or honestly any project on this blog). As such, my implementation of it may have flaws or generous room for improvement. I am not a healthcare professional, nor do I claim to be. Anything I do and describe in my blog is for my personal interest and learning purposes and is not recommended to be used for another’s healthcare. If you find concepts used in this blog that you want to explore for yourself, please seek out and review with your own qualified healthcare professionals.

I believe in complete transparency and honestly about my mental health because it is my way to regain control, help break the stigma, and encourage others to seek out professional help.

This project is not a replacement for any of the medical professionals I see. If you are struggling with mental illness I highly recommend finding your own healthcare professionals. TREATMENT HELPS!!!

I know all of this is likely overkill for a blog nobody reads, but mental health is not something to be played with and I do not want to unintentionally cause harm to anyone on the internet that may stumble across these posts.

Leave a Reply