Coder Social home page Coder Social logo

tensorflow-implementations's Introduction

TensorFlow-Implementations

This repository contains the projects I have done with TensorFlow, and includes a few tutorials.

Check out my Youtube channel which uses some of this material for lessons.

Other social media sites:

Youtube: https://www.youtube.com/channel/UCgRNCT8mrzKYebyG3Ao9DJA/videos

Twitch: https://www.twitch.tv/johncm99

AI Blog: https://delvingintotech.wordpress.com/

LinkedIn: https://www.linkedin.com/in/chong-min-tan-94652288/

Twitter: https://twitter.com/johntanchongmin

TensorFlow / Deep Learning Tutorial: These are the files I have created to teach others Deep Learning:

  • MLP.pdf / MLP Part 2.pdf / CNN.pdf / RNN.pdf / Transformer.pdf / GNN.pdf
  • AlphaGo:Zero.pdf
  • Geometric Deep Learning.pdf (Summary and personal insights of Geometry Deep Learning proto-book https://geometricdeeplearning.com/ by Michael M. Bronstein, Joan Bruna, Taco Cohen, Petar Veličković)
  • Linear Regression in TensorFlow.ipynb
  • Fashion MNIST in TensorFlow.ipynb (Adapted from https://www.coursera.org/learn/introduction-tensorflow Week 1 Notebook)
  • Fashion MNIST (CNN) in TensorFlow.ipynb
  • GAT_GCN_node_classification.ipynb: Graph Neural Network implementation using TensorFlow on Cora Dataset
  • Tutorial_RNN_Text_Generation.ipynb
  • Custom Training Loop: Converts a simple Keras model.fit into an expanded customizable training loop using GradientTape. Includes graph plotting and model visualization utilities.
  • GPT_Demo.ipynb: Uses huggingface GPT-2 model for text generation, as well as Bertviz for attention visualization
  • ChatGPT - From Transformers to Reinforcement Learning from Human Feedback (RLHF).pdf: Introduction to how ChatGPT works

Paper_Reviews Folder:

  • Overview: Documents Interesting Deep Learning / Machine Learning Papers
  • Decision Transformers.pdf
  • GATO.pdf
  • Policy Graident Presentation.pdf
  • AlphaTensor Presentation.pdf

RL Folder:

  • Overview: Documents Reinforcement Learning Experiments and Tutorials
  • RL Part 1.pdf, DQN.pdf
  • MCTS.ipynb: Using Monte Carlo / Monte Carlo Tree Search / Value Estimates to solve the game of Nim
  • Cart Pole.ipynb: Using rule-based and DQN to solve Cart Pole

Below are the projects I have done using TensorFlow (in Implementations folder):

  • Text Generation: Compares RNN vs a Markov Chain method to generate text. Surprisingly, both are around the same, just by using like n=5 previous characters to predict the next one for the Markov Chain method.
  • Text Generation for Code: Compares RNN vs a Markov Chain method to generate text for coding (and also news). Markov Chain methods generate better text, and that could be due to a very small dataset used. Datasets: news.txt, coding.txt
  • If-Else (Part 1): Investigates if a neural network can model if-else statements
  • Wordle: Solves the Wordle game using 5 different agents: Random (baseline), Minimize Worst Outcome, Maximize Entropy (Information Theory), Minimize Variance, Minimize Variance and Worst Outcome. Currently there are only algorithmic agents. In the future, there will also be Deep Q-Networks and Actor-Critic models.

tensorflow-implementations's People

Contributors

tanchongmin avatar

Stargazers

Evan Kurnia Alim avatar evliya-udayev avatar  avatar  avatar Luis Alejandro Smith avatar  avatar Indy Scriabin avatar wenqinl avatar Yinghang Ma avatar  avatar Rushika Madala avatar Marlon Rozindo avatar Juan Rodriguez avatar Concha Labra avatar Vargha Khallokhi avatar  avatar  avatar  avatar Poorna avatar Fanghua(Joshua) Yu avatar  avatar 賴祺清 avatar qqfox avatar Manoj Nair avatar Dustin Rush avatar  avatar  avatar Guangya Wan avatar Jeff Hilton avatar  avatar Amir Lankarani avatar Allan avatar Rudolfo Félix avatar Weatherman avatar Joel Mushagasha avatar Abhishek Bhardwaj avatar TechWithRay avatar Ze Wang avatar  avatar Alexei Korol avatar Dennis Irorere avatar  avatar Zhe Min avatar Sushma Akoju avatar Ze Liu avatar  avatar  avatar Sandalots avatar 爱可可-爱生活 avatar Sigma Quan avatar Liang Depeng avatar Beni Ben zikry avatar  avatar sudo rm -rf --no-preserve-root / avatar sam bacha avatar PtrMan avatar  avatar  avatar Nikolas Markou avatar  avatar  avatar Takuma avatar

Watchers

 avatar yong sri avatar Yinghang Ma avatar  avatar Yongbo Hong avatar Concha Labra avatar

tensorflow-implementations's Issues

error ValueError: too many values to unpack (expected 3) and wrong output for Step 2: Parse Knowledge Graph

LLM with Knowledge Graphs.ipynb

I get error ValueError: too many values to unpack (expected 3) and wrong output for Step 2: Parse Knowledge Graph
https://github.com/tanchongmin/TensorFlow-Implementations/blob/main/Tutorial/LLM%20with%20Knowledge%20Graphs.ipynb

image

and
image

and
image

some updates in your code done to meet changes in updated packages
def strict_output(system_prompt, user_prompt, output_format, default_category = "", output_value_only = False,
model = 'gpt-3.5-turbo', temperature = 0, num_tries = 2, verbose = False):
''' Ensures that OpenAI will always adhere to the desired output json format.
Uses rule-based iterative feedback to ask GPT to self-correct.
Keeps trying up to num_tries it it does not. Returns empty json if unable to after num_tries iterations.
If output field is a list, will treat as a classification problem and output best classification category.
Text enclosed within < > will generated by GPT accordingly'''

# if the user input is in a list, we also process the output as a list of json
list_input = isinstance(user_prompt, list)
# if the output format contains dynamic elements of < or >, then add to the prompt to handle dynamic elements
dynamic_elements = '<' in str(output_format)
# if the output format contains list elements of [ or ], then we add to the prompt to handle lists
list_output = '[' in str(output_format)

# start off with no error message
error_msg = ''

for i in range(num_tries):
    
    output_format_prompt = f'''\nYou are to output the following in json format: {output_format}. 

Do not put quotation marks or escape character \ in the output fields.'''

    if list_output:
        output_format_prompt += f'''\nIf output field is a list, classify output into the best element of the list.'''
    
    # if output_format contains dynamic elements, process it accordingly
    if dynamic_elements: 
        output_format_prompt += f'''

Any text enclosed by < and > indicates you must generate content to replace it. Example input: Go to , Example output: Go to the garden
Any output key containing < and > indicates you must generate the key name to replace it. Example input: {{'': 'description of location'}}, Example output: {{'school': 'a place for education'}}'''

    # if input is in a list format, ask it to generate json in a list
    if list_input:
        output_format_prompt += '''\nGenerate a list of json, one json for each input element.'''
        
    # Use OpenAI to get a response
    response = openai.chat.completions.create(
      temperature = temperature,
      model=model,
      messages=[
        {"role": "system", "content": system_prompt + output_format_prompt + error_msg},
        {"role": "user", "content": str(user_prompt)}
      ]
    )

    #res = response['choices'][0]['message']['content'].replace('\'', '"')
    res = response.choices[0].message.content.replace('\'', '"')
    # ensure that we don't replace away aprostophes in text 
    res = re.sub(r"(\w)\"(\w)", r"\1'\2", res)

    if verbose:
        print('System prompt:', system_prompt + output_format_prompt + error_msg)
        print('\nUser prompt:', str(user_prompt))
        print('\nGPT response:', res)
    
    # try-catch block to ensure output format is adhered to
    try:
        output = json.loads(res)
        if isinstance(user_prompt, list):
            if not isinstance(output, list): raise Exception("Output format not in a list of json")
        else:
            output = [output]
            
        # check for each element in the output_list, the format is correctly adhered to
        for index in range(len(output)):
            for key in output_format.keys():
                # unable to ensure accuracy of dynamic output header, so skip it
                if '<' in key or '>' in key: continue
                # if output field missing, raise an error
                if key not in output[index]: raise Exception(f"{key} not in json output")
                # check that one of the choices given for the list of words is an unknown
                if isinstance(output_format[key], list):
                    choices = output_format[key]
                    # ensure output is not a list
                    if isinstance(output[index][key], list):
                        output[index][key] = output[index][key][0]
                    # output the default category (if any) if GPT is unable to identify the category
                    if output[index][key] not in choices and default_category:
                        output[index][key] = default_category
                    # if the output is a description format, get only the label
                    if ':' in output[index][key]:
                        output[index][key] = output[index][key].split(':')[0]
                        
            # if we just want the values for the outputs
            if output_value_only:
                output[index] = [value for value in output[index].values()]
                # just output without the list if there is only one element
                if len(output[index]) == 1:
                    output[index] = output[index][0]
                
        return output if list_input else output[0]

    except Exception as e:
        error_msg = f"\n\nResult: {res}\n\nError message: {str(e)}"
        print("An exception occurred:", str(e))
        print("Current invalid json format:", res)
     
return {}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.